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 /
  • Help Room /
avatar image
0
Question by TheYolocaust · Jul 14, 2016 at 02:52 PM · c# tutorial

Array indexing problem

I am trying to get my code to cycle through an array when I press a button and it works for the most part, but some of the colors don't change. For instance, the order is supposed to be: white, red, orange, yellow, green, blue, purple, pink, and then back to white. The order it actually goes in is: white, red, yellow, yellow, green, blue, pink, pink, and then back to white.

 using UnityEngine;
 using System.Collections;
 
 public class InputScript : MonoBehaviour {
 
     public KeyCode UpKey = KeyCode.UpArrow;
     public KeyCode DownKey = KeyCode.DownArrow;
     public KeyCode ChangeColorKey = KeyCode.Backslash;
     SpriteRenderer Sprite;
 
     int CurrentColor = 0;
 
     Color[] Colors;
 
     PongPaddleScript PaddleScript;
 
     // Use this for initialization
     void Start () {
         PaddleScript = GetComponent<PongPaddleScript>();
         Sprite = GetComponent<SpriteRenderer>();
 
         Colors = new Color[8];
         // White
         Colors[0] = new Color(255, 255, 255, 255);
         // Red
         Colors[1] = new Color(255, 0, 0, 255);
         // Orange
         Colors[2] = new Color(255, 102, 0, 255);
         // Yellow
         Colors[3] = new Color(255, 255, 0, 255);
         // Green
         Colors[4] = new Color(0, 255, 0, 255);
         // Blue
         Colors[5] = new Color(0, 0, 255, 255);
         // Purple
         Colors[6] = new Color(128, 0, 255, 255);
         // Pink
         Colors[7] = new Color(255, 0, 238, 255);
     }
     
     // Update is called once per frame
     void Update ()
     {
         if(Input.GetKey(UpKey))
         {
             PaddleScript.setDirection(1);
         }
         else if(Input.GetKey(DownKey))
         {
             PaddleScript.setDirection(-1);
         }
         else
         {
             PaddleScript.setDirection(0);
         }
 
         if(Input.GetKeyDown(ChangeColorKey))
         {
             if(CurrentColor + 1 > Colors.Length - 1)
             {
                 CurrentColor = 0;
             }
             else
             {
                 CurrentColor += 1;
             }    
             Sprite.color = Colors[CurrentColor];
         }
     }
 }
 
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
0
Best Answer

Answer by Jessespike · Jul 14, 2016 at 08:02 PM

Each color component is a floating point value with a range from 0 to 1.

https://docs.unity3d.com/ScriptReference/Color.html

You can convert the values to a floating point between 0 and 1, or divide the current values with 255.

      // Orange
      //Colors[2] = new Color(255, 102, 0, 255);

      Colors[2] = new Color(255f/255f, 102f/255f, 0f/255f, 255f/255f);
      Colors[2] = new Color(1f, 0.4f, 0f, 1f);
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 TheYolocaust · Jul 14, 2016 at 09:48 PM 0
Share

Odd solution, but thank you. It worked.

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

61 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

Related Questions

getting unexpected symbol ';' error 1 Answer

How to Move an Image to an other Panel 1 Answer

Best way to create a currency converter in Unity with C# 0 Answers

A way to make the player get punished when standing still? 0 Answers

Is there a way to run Destroy(gameObject) after the game has run the previous code? 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