- Home /
Instant Transform.Rotate?
I want to make a light switch, and want to use transform.rotate to make the button turn from 14,0,0 to -14,0,0 and vice versa, but my script keeps bringing up the following errors:
Assets/LightSwitchRotation.cs(8,23): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer
Assets/LightSwitchRotation.cs(11,28): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer
This is my script:
using UnityEngine;
using System.Collections;
public class LightSwitchRotation : MonoBehaviour {
private void OnMouseDown()
{
if (transform.Rotate = (new Vector3(-14,0,0)))
{transform.Rotate(new Vector3(14,0,0));}
else if (transform.Rotate = (new Vector3(14,0,0)))
{transform.Rotate(new Vector3(-14,0,0));}
}
}
Answer by perchik · Aug 29, 2013 at 02:15 PM
Your comparisons are incorrect.
First you need to use ==
instead of =
in an if statement. A single equal sign will assign a value to something; double equal signs compare if two things are the same. This is what your error is actually referring to, because the compiler sees transform.Rotate = new Vector3(-14,0,0)
and it says "Oh, you are trying to assign a vector to a function, that's not good!"
Second, transform.Rotate
is a function that is used to rotate an object by some amount, so it doesn't make sense to use it in the comparison statements. Instead, you probably want transform.rotation.eulerAngles
or transform.eulerAngles
. Both do the same thing.
Therefore, you code should look like this:
private void OnMouseDown()
{
if (transform.eulerAngles == new Vector3(-14,0,0))
{
transform.Rotate(new Vector3(14,0,0));
}
else if (transform.eulerAngles == new Vector3(14,0,0))
{
transform.Rotate(new Vector3(-14,0,0));
}
}
You could also just check to see if transform.eulerAngles.x == -14
instead