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
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.
Answer by NoseKills · Aug 11, 2016 at 06:32 PM
In case you are wondering why playerColor
is 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
Your answer

Follow this Question
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