• 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 /
  • Help Room /
avatar image
0
Question by Olliepm · Nov 09, 2015 at 12:41 PM · scripting beginner

Change main colour of all prefab instances from C# script?

I have created a prefab called DestroyPlane (for killing player on collision). I'm interested in changing the main colour of all instances of DestroyPlane from a C# script. This paramater is shown by the green arrow in the picture below.

If it makes any difference, I plan on controlling these colour changes from a script that is not attatched to DestroyPlane, but to another object which on collision will cause the colour to change.

Can anyone tell me how I would access the the prefab colour and change it to a new colour from a C# script so that all instances used in the level change colour at the same time?

alt text

pre.jpg (102.3 kB)
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by OctoMan · Nov 09, 2015 at 03:05 PM

Once the collison happens, just search the scene for tagged objects to change values on them.

You can store all found in a list and then loop through the list, change all stuff and then remove the item from the list. Or use an array and a foreach to do the stuff.

How ever the approach could be:

 void OnTriggerEnter(Collision other)
     {
         if(other.collider.tag == "whatever")//whatsever collides
         {
             GameObject[] destroyPlanes = GameObject.FindGameObjectsWithTag ("destroyPlane");//create an array of gameobjects
             {
                 foreach (GameObject plane in destroyPlanes)//go through em
                 {
                     plane.GetComponent<Renderer>().material.color = new Color32();//set your color here
                 }
             }
         }
     }

It could contain problems, but thats the basic idea, how i would try to approach this.

Comment
Add comment · 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
0

Answer by OncaLupe · Nov 09, 2015 at 03:22 PM

You have two options that I can think of. If you can assign the material directly to the script that will be changing the color, then it's an easy change.

 public Material destroyPlaneMaterial;
 void ChangeMaterialColor()
 {
      destroyPlaneMaterial.color = Color.red;
 }

However, if you're unable to do this for whatever reason (multiple types of destroy planes, texture could change during runtime, etc) then you can access this through the Renderer's 'sharedMaterial' variable.

 public GameObject destroyPlane;
 void ChangeMaterialColor()
 {
      destroyPlane.GetComponent<Renderer>().sharedMaterial.color = Color.green;
 }

If you need to be able to do this repeatedly, then either try to use the first method, or at least store the Renderer reference for use in the second so you're not calling GetComponent unnecessarily.

Note for both of these: Since you're changing the material directly, this is saved even after exiting Play mode, so you'll need to reset the color on game start. Also, if you're using this material on any other objects, they will also be changed. Make sure the material is only used on destroy planes that you want to modify.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

33 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 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

Unity {[(2D)]} RotateAround while jumping 0 Answers

Why do I only have left/right mouse movement 1 Answer

2D Frogger Logs and Water issues 0 Answers

How do i make my enemies face the player? 2D 0 Answers

I want to have a UI only pop up when you press the player button or when you open up the Unity executables. 1 Answer


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