Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by javim93 · Feb 12, 2014 at 01:57 PM · prefabmaterialschange

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();
 }
  
  
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Feb 12, 2014 at 02:13 PM 0
Share

What indicates that this game object is the one you are using at the moment vs. the other game objects in the scene?

avatar image javim93 · Feb 12, 2014 at 02:28 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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);
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image javim93 · Feb 12, 2014 at 03:31 PM 0
Share

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(); } }

avatar image nesis · Feb 13, 2014 at 02:35 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges