What I want to do:
I have a cube that once a character object collides with it, I want it to call the Action() method of a list of objects associated with it, for example A Door Object will go up or down as in opening.
The issue:
On the Action method I have this line:
gameObject.transform.position += new Vector3(0, 1, 0);
which correctly moves the object a few units up.
However, what I want its for this to work:
gameObject.transform.position += new Vector3(0, direction, 0);
direction *= -1;
Essentially have a variable that changes from up to down each time Action is called.
This is the fullscript:
public class DoorHandle : MonoBehaviour
{
// Start is called before the first frame update
int direction;
void Start()
{
direction = 1;
}
public void Action()
{
Debug.Log("Calling Action");
gameObject.transform.position += new Vector3(0, direction, 0);
// Reverse direction for the next action call
direction *= -1;
}
}
For somereason, this doesn’t work. The Y position doesn’t change at all.
What I have tried:
I have tried changing it to this.direction, I have changed it from public to private, I have assigned at Class level. I have no idea why it doesn’t work when I use the variable direction and why it works if I just hard code it, I have a similar object that calls a a similar function with Update and it moves correctly.