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
0
Question by hollym16 · Dec 17, 2014 at 03:37 PM · gameobjectraycastcolorstring

Click GameObject to change Color

I want to code so that when I click on a GameObject, it changes colour with each click. For example, it starts off white: click to turn red, click again and it turns green, and so on and so forth until it get back to white.

I've got a single click working with Raycast. I'm guessing I'd need to do a string perhaps?

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
1
Best Answer

Answer by Kartik1607 · Dec 18, 2014 at 12:41 PM

Hey there, You don't need a loop to loop through color's. Use a variable to store current index of color and one to store the length of color array.

     Color[] colors = new Color[] {Color.white, Color.red, Color.green, Color.blue};
     private int currentColor,length;
     // Use this for initialization
 
     void Start () 
     {
         currentColor = 0; //White
         length = colors.Length;
         renderer.material.color = colors [currentColor];
     }

In update, when ray hit's increment the currentColor by 1. To make sure, it does not get above the max length, use the remainder operator.

 void Update () {
         
         if(Input.GetMouseButtonDown(0))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, 100))
             {
                 currentColor = (currentColor+1)%length;
                 renderer.material.color = colors[currentColor];
             }
         }
     }


Change the script as per your code.

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 Cherno · Dec 17, 2014 at 04:27 PM

You need to declare an array of colors to cycle through, first:

 Color[] colors = new Color[] {Color.white, Color.red, Color.green, Color.blue};
 

Then, after the objects is hit by your RC, you call the following code:

 Color goColor = yourObject.renderer.material.color;
 for(int i = 0; i < colors.Length; i++) {
      
      if(goColor == colors[i] && i == colors.Length - 1) {
           yourObject.renderer.material.color = colors[0];
           Return;
      }
      else {
           yourObject.renderer.material.color = colors[i + 1];
           Return;
      }
 }

There are probably better ways to do it but that should get you started.

Comment
Add comment · Show 1 · 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 hollym16 · Dec 18, 2014 at 11:46 AM 0
Share

I've created the following script:

 using UnityEngine;
 using System.Collections;
 
 public class colourchange : $$anonymous$$onoBehaviour {
 
     private GameObject Cube;
     Color[] colors = new Color[] {Color.white, Color.red, Color.green, Color.blue};
 
     // Use this for initialization
     void Start () {
 
         Cube = GameObject.Find("Cube");
     }
     
     // Update is called once per frame
     void Update () {
 
         if(Input.Get$$anonymous$$ouseButtonDown(0)){
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, 100)){
                 if(hit.transform.name=="Cube"){
                     Color goColor = Cube.renderer.material.color;
                     for(int i = 0; i < colors.Length; i++){
                         if(goColor == colors[i] && i == colors.Length - 1){
                             Cube.renderer.material.color = colors[0];
                             Return;
                         }
                         else{
                             Cube.renderer.material.color = colors[i+1];
                             Return;
     }
 }
                 }
             }
         }
     }
 }

However, I get the following error, regarding the 'Return' functions:

 Only assignment, call, increment, decrement, and new object expressions can be used as a statement

If i remove these two 'Returns', the script doesn't work; I click on the object and nothing happens. Any suggestions on what I may be doing wrong?

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

31 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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

changing guiTexture color with raycast 1 Answer

Raycasting from the sides of a GameObject 2 Answers

GameObject.name to string 2 Answers

Drag Rigid Body No Distance Limit and how to add Mouselock to a scrip 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