- Home /
The question is answered, right answer was accepted
Script works with hard coded values but not variables
I have a world wrap script that works flawlessly, but if I try to replace the values with variables of EXACTLY the same value, everything falls apart. I even tried replacing one value at a time and checking it to be extra careful.
Here's the script. The variables at the top represent the values in the function.
public int rightWall = 100;
public int leftWall = -110;
public int topWall = 190;
public int bottomWall = 50;
void Update ()
{
if (transform.position.x <= -110)
{
transform.position = new Vector3(100, transform.position.y, 0);
}
if (transform.position.x >= 101)//+1
{
transform.position = new Vector3(-109, transform.position.y, 0);//+1
}
if (transform.position.y >= 190)
{
transform.position = new Vector3(transform.position.x, 50, 0);
}
if (transform.position.y <= 49)//-1
{
transform.position = new Vector3(transform.position.x, 189, 0);//-1
}
}
The comments represent what I would add or subtract from the variables so that the objects are within the boarders when their position is changed to the other side.
Do you get any errors when replacing the values with variables? Vector3 takes float values as input, but your variables are ints.
I do not get any errors, but I tried using floats ins$$anonymous$$d anyways, it still doesn't work.
The variables are public. Are you sure you haven't changed the values outside this script? Look at the values in the inspector or/and Debug.Log them.
What about setting up a temp Vector3 that has the variables.
Something like
Vector3 rightWallPos = new Vector3(rightWall, transform.position.y, 0);
//and then set the transform
transform.position = rightWallPos;
I don't know if that will work, and I have no idea why it won't work with the variables :O Curious..
Answer by Hydropulse17 · Feb 03, 2016 at 07:20 AM
@saschandroid solved my issue in the comments. My variables were public, and were different on the object prefabs from the script. I didn't change them, I initiated them in the script and never touched the inspector, but that was in fact the problem. Resetting the monobehavior component on the prefab fixed my issue.
Answer by MrMatthias · Feb 03, 2016 at 06:45 AM
I'm not sure what you are trying to do, but you set the position to fixed positions, there won't be any movement. To add or subtract values use something like this:
transform.position += new Vector3 (1, 0, 0); // move by 1 in x direction
you might also want to make it dependent on Time.deltaTime otherwise the speed will depend on the speed of the running pc.
transform.position += new Vector3 (xSpeed * Time.deltaTime, 0, 0);
It needs to teleport to the other side, a whole new position is being assigned. $$anonymous$$y script is not flawed, like I've said, it works fine without variables in place of the hard values.
Thank you for your answer anyways, because that second tip will actually help me with another problem I've been having.
Follow this Question
Related Questions
transform.position not adding correctly with a Vector3 1 Answer
Object doesn't move 2 Answers
How to use a Vector3.MoveTowards and the resulting position as an input for a normalised BlendTree 0 Answers
Get an Object to move to a Position for a Fixed time 2 Answers
How to teleport gameobject (instantly change transform.position) 3 Answers