- Home /
How to change material of the active object?
Hi, im using a script that changes material of the object when a key is press. The problem is that i have the same object many times in the same scene(its a prefab that cudnt be changed). I need the script to change just the object prefab im using at the moment. Any ideas?
var myMaterials : Material[];
var NextMaterial : int = 0;
function NextMaterialUsed()
{
renderer.sharedMaterial = myMaterials[NextMaterial];
if(NextMaterial < myMaterials.length-1)
NextMaterial += 1;
else
NextMaterial = 0;
}
function Update ()
{
if(Input.GetButtonDown("Jump"))
{
NextMaterialUsed();
}
}
What indicates that this game object is the one you are using at the moment vs. the other game objects in the scene?
the cube is part of a prefab used as player. Its a multiplayer game, and it use the same prefab(that includes the cube) to create the character for both players. When i change with script the material of the cube in the current player, it also changes the other player texture cube. That is the problem, there is no diference between the current cube and the other player cube
Answer by nesis · Feb 12, 2014 at 02:28 PM
Don't set sharedMaterial, if you change that, it'll change that material's settings in your project even if only done during a play test in the Editor. Use renderer.material during play. That will also only modify the material for the given object only.
Just in case you (or someone else reading this) is trying to assign materials through code, to make your renderer's material unique so modifying it won't affect other objects, do this:
renderer.material = new Material(someCustomMaterial);
Using new Material(someMaterialVariable)
will create a completely new, separate material that copies the settings of someMaterialVariable
exactly. This is handy to know if you're assigning the same material to several objects via code, and want it to be able to be tweaked separately per object. After (and only after!) using the line above, you can safely use renderer.material to modify the material's settings. As an example:
var someMaterial : Material;
function Awake() {
//create an instance of your renderer's material here
renderer.material = new Material(someMaterial);
}
function Update() {
//Modify a setting for renderer.material over time.
//Notice that same setting won't change for other materials.
var offset : float = Time.time * scrollSpeed;
renderer.material.mainTextureOffset = Vector2 (offset, 0);
}
I changed shared material, but the problem persist. Its a fps game, and it use the same cube for all players. In another script theres is a function that sais "if(Player.networkView.is$$anonymous$$ine==true){" that makes respwan the current cube, but i cant include it on that script. I paste the code modificated as you sujested.
var some$$anonymous$$aterial : $$anonymous$$aterial[]; var Next$$anonymous$$aterial : int = 0; function Next$$anonymous$$aterialUsed() { renderer.material = some$$anonymous$$aterial[Next$$anonymous$$aterial]; if(Next$$anonymous$$aterial < some$$anonymous$$aterial.length-1) Next$$anonymous$$aterial += 1; else Next$$anonymous$$aterial = 0; }
function Update () { if(NetworkHealth.dead == true){ Next$$anonymous$$aterialUsed(); } }
If you're using assigning a new material using only new $$anonymous$$aterial(some$$anonymous$$aterial)
, you'll not run into issues of materials being shared between objects. Are you saying you're spawning a new cube each time a player respawns?
If's the case, a couple of options spring to $$anonymous$$d:
1) Ins$$anonymous$$d of destroying the old cube and instantiating a new one, just re-use the old cube by resetting its position, orientation, health, etc. You'd do best to add a Respawn() method to your player's script(s), then use myPlayerGameObject.Send$$anonymous$$essage("Respawn") on your player's GameObject when they die. That'll call all methods called Respawn() in all scripts attached to your player's GameObject.
2) If you're still wanting to destroy the old cube and instantiate a new one, use the code I've given you (in my Awake() method) in an Awake() or Start() method.
Your answer
Follow this Question
Related Questions
Prefabs losing Material connections after re-opening scenes. Bug? 3 Answers
Changing the color of a material during runtime (works in editor, not build) 1 Answer
How do you associate a Material Picker to prefab placed in scene from AssetBundles 0 Answers
How would I randomize the materials of an object using this script. 1 Answer
How to change all materials of object and then change it back to normal? 0 Answers