x


Change mainTexture of new Instantiate

But keep previous Instantiate's texture the same..?

I'm completely stumped by this, I can instance the same object with a different texture but the issues comes when it changes the previous instance's texture.

An example of what I mean is, instantiating a prefab called Chap, like: Instantiate (Chap) with a blue texture, then Instantiate (Chap) again, with a pink texture.. Make sense?

Anyone have any ideas or any links to code references that might help? Cheers.

EDIT: Sorry, I'm not being very clear I'm having trouble explaining what I mean. I have a prefab of a person, with an unlit blue texture, I want to be able to instantiate the same prefab, in a different place with a different texture.

var Selected : int = 0;
var Jeff : GameObject;
var Blue : Texture;
var Pink : Texture;

function OnMouseUp (){
    if (Selected == 1){
        Instantiate (Jeff);
        Jeff.renderer.material.mainTexture = Blue;
    }
    if (Selected == 2){
        Instantiate (Jeff);
        Jeff.renderer.material.mainTexture = Pink;
    }
}

The problem is with not changing the previous instatantiated texture aswell. Probably should have mentioned in JavaScript, so I apologize for that.

more ▼

asked Jun 14 '11 at 02:01 PM

EskimoEthics gravatar image

EskimoEthics
1 1 1 2

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Aha! Managed to find some code already posted here that pretty much did what I need:

function changeMaterial01(){
    var changeMaterial01 : Material = Jeff.renderer.material;
    Jeff.renderer.material = Material01;
}
more ▼

answered Jun 14 '11 at 04:38 PM

EskimoEthics gravatar image

EskimoEthics
1 1 1 2

Can you explain what you did in the end. I think im doing the same thing and want to have multiple "jeff" instances with different textures. How did you do it in the end? Thanks.

Jul 13 '11 at 09:33 AM jammerjar
(comments are locked)
10|3000 characters needed characters left

Hi There! If I understand what you're saying, you want multiple instantiated objects with the same texture? That is quite possible. Here's a quick demo (requires a gameobject and 3 textures).

using UnityEngine;

public class lol : MonoBehaviour
{

public GameObject MyPrefab;
public Texture[] Textures = new Texture[] { null, null, null };
private GameObject[] _prefabs = new GameObject[3];
void Start()
{
    _prefabs[0] = (GameObject)Instantiate(MyPrefab, Vector3.zero, new Quaternion(0, 0, 0, 0));
    _prefabs[1] = (GameObject)Instantiate(MyPrefab, Vector3.left, new Quaternion(0, 0, 0, 0));
    _prefabs[2] = (GameObject)Instantiate(MyPrefab, Vector3.right, new Quaternion(0, 0, 0, 0));

    for (int i = 0; i < _prefabs.Length; i++)
    {
        GameObject prefab = _prefabs[i];
        prefab.GetComponent<MeshRenderer>().material.mainTexture = Textures[i];
    }
}
}

Here's some things to note: Each time you change the texture on a gameobject when unity is running, Unity creates a temporary instantiated material of that object. This means your changes don't go to the hard drive, and you're pretty much making each object completely seperate (think individual draw-calls if you want to use this on a phone or iPad in the future).

You can't directly access an objects material, as it is rendered through either a Mesh Renderer or Skinned Mesh Renderer (for characters). A material isn't a component, so you can't use GetComponent on it directly.

more ▼

answered Jun 14 '11 at 02:34 PM

kariwmklawm gravatar image

kariwmklawm
61 4

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2203
x1674
x5

asked: Jun 14 '11 at 02:01 PM

Seen: 1909 times

Last Updated: Jul 13 '11 at 09:33 AM