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 /
This question was closed May 09, 2015 at 10:22 PM by joshua-lyness for the following reason:

Duplicate Question

avatar image
0
Question by joshua-lyness · May 06, 2015 at 07:53 PM · materialinspectormaterialsruntime

Changing material during runtime

I want to change the material of some trees to add variation to my scene. By clicking the mouse, you instantiate a tree on the ground mesh. I have the following script attached to my trees, that should change the colour of them:

 public class treeSettings : MonoBehaviour 
 {
     public Material[] myMaterials;
 
     void Start () 
     {
         gameObject.GetComponent<Renderer>().material = myMaterials[Random.Range(0,myMaterials.Length)];
     }
 }

In the inspector, the material changes, however in the game, it does not.

Thanks for any help you can give me.. im really stumped. :(

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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by scarletshark · May 06, 2015 at 08:23 PM

Are you 100% you've set your materials in the inspector? I copy/pasted your code and it worked fine using my own instantiation script:

     public GameObject cube;
     private Vector3 clickPosition;
     private Vector3 cubePosition;
     private float zDistance;
     
     
     // Update is called once per frame
     void Update () {
         if(Input.GetMouseButtonDown(0)){
             
             zDistance = Random.Range(2.0f, 10.0f);
             
             Vector3 clickPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, zDistance);
             
             cubePosition = Camera.main.ScreenToWorldPoint(clickPosition);
             
             Instantiate(cube, cubePosition, Quaternion.Euler(Vector3.zero));
         }
     }

alt text


screen-shot-2015-05-06-at-41600-pm.png (150.7 kB)
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 joshua-lyness · May 06, 2015 at 09:59 PM 0
Share

well what I did was instantiate my object like the above, and then in a separate script, I had that code. I added a meshRenderer component to the object, and then that code to each. However, when I run the game, the material appears in the inspector, but isn't applied to the object. It stays pink... An screenshot of what happens: alt text

screenshot-26.png (244.5 kB)
avatar image joshua-lyness · May 09, 2015 at 10:24 PM 0
Share

Figured out what I was doing wrong, I had a prefab within a prefab, and it was instantiating both, with the child for some reason forcing the parent not to render... Anyway, solution found, thanks everyone that helped.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

How to change a specific material of an object ? 0 Answers

Shader: get back scene pixel color? 1 Answer

change material upon collision 1 Answer

Material is not visible in android build but works fine in editor? 0 Answers

How to use 3rd Party Shader with LWRP / URP 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