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 boatkay · Aug 11, 2016 at 05:21 AM · spritefunction

Function to set enum is not working right.

Hello, I have a script on a game object which changes sprites randomly on trigger. I also have another function sets the color of the object from an enum. The function is supposed to checks the sprite of the game object and based on that it will set the color of the game object. But this function has not been working properly and also interfering in the the changing of the sprite of the gameobject. and now the game object is set on the same color throughout the game. This is my code for the gameobject: public class PlayerController : MonoBehaviour {

     public Sprite[] playerSprites = new Sprite[4]; //sprites to be used by player game object
     //playerSprite[0] = Red, playerSprite[1] = Pink,                                                    //playerSprite[2] = Purple, playerSprite[3] = Blue
     public ObjectColor playerColor;
     public int spriteIndex; //Random number to access sprites.
     SpriteRenderer sr;
     Rigidbody2D rb;
     public float gameSpeed;
     // Use this for initialization
     void Start ()
     {
         sr = GetComponent <SpriteRenderer> ();
         rb = GetComponent <Rigidbody2D> ();
         SetOrChangeSprite ();
         Debug.Log (sr.sprite);
         Debug.Log (SetColor (playerColor));
     }
     
     void FixedUpdate ()
     {
         MovePlayer ();
     }
 
     //Set or change the sprites of the player game object at runtime
     public void SetOrChangeSprite ()
     {
         spriteIndex = Random.Range (0, 4);
         sr.sprite = playerSprites [spriteIndex];
     }
 
     //Set the color of the player game object to an enum type
     ObjectColor SetColor (ObjectColor color)
     {
         if (sr.sprite = playerSprites [0])
         {
             color = ObjectColor.Red;
         }
         else if (sr.sprite = playerSprites [1])
         {
             color = ObjectColor.Pinkish;
         }
         else if (sr.sprite = playerSprites [2])
         {
             color = ObjectColor.Purple;
         }
         else if (sr.sprite = playerSprites [3])
         {
             color = ObjectColor.Blue;
         }
 
         return color;
     }
 
     void MovePlayer ()
     {
         rb.velocity = (new Vector2 (rb.velocity.x, 1f)) * gameSpeed;
         //transform.position += Vector3.up * gameSpeed * Time.deltaTime;
     }
 }
 

I have been trying to solve this problem since morning coming up with different solutions but nothing seems to be working. Could someone help me figure out whats wrong. And why the function does not set the player color and also prevents the sprites from being random. But when I delete the set color function, everything works like it should

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
0

Answer by btmedia14 · Aug 11, 2016 at 12:18 PM

@boatkay The first thing to notice is in the SetColor() function the conditions for each of the if statements are actually assignments and not logical equalities. The = sign should be == for each of those if statements. So the sr.sprite value is being reassigned inside the SetColor() function, most likely to the first statement. This will cause the expected randomness to be effected too.

Also, just fyi, the ObjectColor parameter passed into the SetColor() function doesn't actually get used inside that function. The color variable is simply being assigned a value and returned.

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 NoseKills · Aug 11, 2016 at 06:32 PM

In case you are wondering why playerColoris not changing in the inspector when you run this, it's because enum variables are value types just like int, float or bool to mention a few.

That's why you can't make a function that would take in playerColor and change its value inside the function. The color variable inside the function and playerColor are not connected as they would be when dealing with reference types. Instead color is just a copy of playerColor and all you do with it is return it and Debug.Log the returned value.

Also as @btmedia14 noted, your if's have assignments in them, not logical comparisons.

The value of an assignment is the rightmost operand so

 if (sr.sprite = playerSprites [0])

is true if playerSprites[0] is not null/destroyed

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

82 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

Related Questions

How can I make Image border not change a size? 1 Answer

Creating a 2D Maze 0 Answers

Rebuild Roguelike with other sprite sheet size 1 Answer

Sprite sheet and animation slice doubt 0 Answers

sprite(missing) at runtime 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