• 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
Question by Siloxis · Mar 31, 2020 at 11:01 PM · scripting problemcolor

Totally new, is there something wrong with this switch script for color changes?

I am still learning on my own with tutorials so any knowledge and direction would be amazing. The lesson I was going over was meant to teach using switches to change the color of an object. However, not$$anonymous$$ng I do makes the object respond as it did in the video. Is there somet$$anonymous$$ng my script is missing for that simple purpose? I am able to run it without being met with compiling errors or console problems so I am lost.

Here is what I have got, any suggestions?

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

public class ColorChange : MonoBehaviour {

 public GameObject cube;
 private int _nextColor;
 // Start is called before the first frame update
 void Start()
 {
     _nextColor = 0;
 }

 // Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Alpha1))
     {
         _nextColor = 0;
     }

     if (Input.GetKeyDown(KeyCode.Alpha2))
     {
         _nextColor = 1;
     }

     if (Input.GetKeyDown(KeyCode.Alpha3))
     {
         _nextColor = 2;
     }

     if (Input.GetKeyDown(KeyCode.Alpha4))
     {
         _nextColor = 3;
     }

     switch (_nextColor)
     {
         case 0:
             cube.GetComponent<Renderer>().material.color = Color.blue;
             break;
         case 1:
             cube.GetComponent<Renderer>().material.color = Color.red;
             break;
         case 2:
             cube.GetComponent<Renderer>().material.color = Color.green;
             break;
         case 3:
             cube.GetComponent<Renderer>().material.color = Color.black;
             break;
            
     }
 }

}

Comment

People who like this

0 Show 0
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
Best Answer

Answer by The_Three_Vs · Mar 31, 2020 at 11:28 PM

When I copied the code I was able to make the code work perfectly without any changes.

Make sure that the script is actually attached to a gameObject. Other than that, I can't t$$anonymous$$nk of any reason why t$$anonymous$$s wouldn't be working. Can you provide more details about the cube and the object the script is attached to?

Comment
Siloxis

People who like this

1 Show 3 · 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 Siloxis · Apr 01, 2020 at 12:21 AM 0
Share

Now I am even more confused. After reading your message I created a new project and applied the code to a cube without any problems. The original project file has all my practice material on it, is it possible that some conflicts do not show up in the console? Currently concerned with recreating the problem as projects get bigger.

avatar image The_Three_Vs Siloxis · Apr 01, 2020 at 01:16 AM 0
Share

Was the original project created in a different version? Though Unity doesn't change majorly over updates, small things do get changed, which might affect the function of material.color. Other than that, I'm not sure what the issue could be.

avatar image Siloxis The_Three_Vs · Apr 01, 2020 at 04:05 AM 0
Share

Hard to say for sure, but I appreciate the help nonetheless! Thank you.

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

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

get color of randomly generated objects 1 Answer

Create new mesh from script always black color even i change material. 1 Answer

Sprite dots spawn and have their own color, but they only show as wireframes? 0 Answers

A Javascript trying to access an image effect C# script 1 Answer

I cant find out what will fix this code it has stumped me for a week now with no solution that I can find 2 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