Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by soyeon9897 · Sep 26, 2018 at 10:22 PM · collisioncolorrandom.rangecolor change

Collision issue

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class WhiteSheep : MonoBehaviour { Renderer _renderer; //Declaring _renderer public GameObject[] trees; //Declaring trees for OnCollisionEnter

 void Awake()
 {
     _renderer = GetComponent<Renderer>();
     //This is to not repeat GetComponent<Renderer>();
 }

 IEnumerator Start()
 //This is so that I can use a loop in a Start().
 {
     for (int i = 0; i < 40; i++)
     //This will make it repeat changing its colour for 40 times.
     {
         int colour = getRandomInt(0, 3);
         //There are three different colours that it can change into.

         if (colour == 1)
         {
             Renderer rend = GetComponent<Renderer>();
             //so we can access the colour

             rend.material.shader = Shader.Find("_Color");
             rend.material.SetColor("_Color", Color.black);

             rend.material.shader = Shader.Find("Specular");
             rend.material.SetColor("_SpecColor", Color.black);
             //Find the Specular shader and change its Color to black
             yield return null;
             //This is for Coroutines so it loops.
         }

         else if (colour == 0)
         {
             Renderer rend = GetComponent<Renderer>();
             //so we can access the colour

             rend.material.shader = Shader.Find("_Color");
             rend.material.SetColor("_Color", Color.gray);

             rend.material.shader = Shader.Find("Specular");
             rend.material.SetColor("_SpecColor", Color.gray);
             //Find the Specular shader and change its Color to gray
             yield return null;
             //This is for Coroutines so it loops.
         }

         else if (colour == 2)
         {
             Renderer rend = GetComponent<Renderer>();
             //so we can access the colour

             rend.material.shader = Shader.Find("_Color");
             rend.material.SetColor("_Color", Color.red);

             rend.material.shader = Shader.Find("Specular");
             rend.material.SetColor("_SpecColor", Color.red);
             //Find the Specular shader and change its Color to red
             yield return null;
             //This is for Coroutines so it loops.
         }
     }

     
 }

 private int getRandomInt(int start, int end)
 //This is to make getRandomInt as a function
 {
     float fRandom = Random.Range(start, end);
     int iRandom = Mathf.FloorToInt(fRandom);
     return iRandom;
 }


 private void OnCollisionEnter(Collision collision)
 {
     if (collision.collider.CompareTag ("Player"))
     {
         foreach (GameObject tree in trees)
         //Foreach loop is to block a statement for each element
         {
             int colour = getRandomInt(0, 2);

             if (colour == 1)
             {
                 Renderer rend = tree.GetComponent<Renderer>();

                 rend.material.shader = Shader.Find("_Color");
                 rend.material.SetColor("_Color", Color.yellow);

                 rend.material.shader = Shader.Find("Specular");
                 rend.material.SetColor("_SpecColor", Color.yellow);
                 //Find the Specular shader and change its Color to yellow
             }

             if (colour == 0)
             {
                 Renderer rend = tree.GetComponent<Renderer>();

                 rend.material.shader = Shader.Find("_Color");
                 rend.material.SetColor("_Color", Color.red);

                 rend.material.shader = Shader.Find("Specular");
                 rend.material.SetColor("_SpecColor", Color.red);
                 //Find the Specular shader and change its Color to red
             }
         }
     }
 }

}

I want to make a tree to change its colour when the player collides into the WhiteSheep. The colour of the sheep changes perfectly, but when the player collides with the sheep, tree colour doesn't change. But no errors appear in Unity... Can you please help me?

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

· Add your reply
  • Sort: 
avatar image
1

Answer by GraemeJudkins · Sep 27, 2018 at 12:03 AM

You could try changing the "_emissionColor" in the material, open up the material inspector during runtime and see if changing any of the material properties changes the colour of the tree in unity. You will need to open the debug inspector to do so You will need to open the debug inspector to do so The material will display extra options, at the bottom will be material colours The material will display extra options, at the bottom will be material colours


2018-09-26-1759.png (27.2 kB)
2018-09-26-1800.png (27.3 kB)
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.

Follow this Question

Answers Answers and Comments

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

Array of colors not working, all sprites turn white. 1 Answer

How to change colors in turn? 1 Answer

Transparent texture and object with changing color 0 Answers

How do I change collision according to gameobject - ground color? 1 Answer

Change Colour Map colours using struct 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