Unity Hanging when Changing a Vector3.x
Hey. I have a pretty simple script which modifies the position of a gameobject based on the position of a 'target' object. Code is as follows, and is called in the FixedUpdate function:
Vector3 pos = gameObject.transform.position;
pos.y = target.transform.position.y + (0.33f + (0.55f * randYFactor));
pos.x = target.transform.position.x - ((2 + (1 * randXFactor)) * dir);
Vector3 newPos = Vector3.MoveTowards(gameObject.transform.position, pos, 0.01f);
float addY = Mathf.Sin((Time.time * 10) * height) / 75;
newPos.y += addY;
gameObject.transform.position = newPos;
For some reason, the third line of this script seems to be causing unity to crash. When it is commented out, there doesn't seem to be an issue, and even if I remove all the variables to make it as simple as pos.x = target.transform.position.x, the game still crashes. The game does not crash right away, but it always does when that one line is there and I can't find any other events that could also be effecting this.
Any help is appreciated, thanks.
Answer by Bunny83 · Nov 18, 2017 at 03:41 AM
I highly doubt that this line can cause your game or Unity to crash. Maybe the value you set has another effect in some other script you have. Also you should be more specific. Do you actually experience a crash or a hang? If you get a crash, what does the crash report say? If it's a hang it's most likely that you have an infinite loop somewhere in your application which might depend on the x position of this object.
Depending on your system you can use VisualStudio or MonoDevelop and attach it as debugger. This allows you to set breakpoints and to step through the code line by line.
Note that if you get stuck in an infinite loop and Unity will hang as a result, you won't see any debug messages you may have posted during this frame.
Thank you so much. It is a hang, my bad. And I think it might have to do with another loop elsewhere. I didn't consider it because it's not looking at the target object, but its another object looking at the one in question. Only one way to find out,
Answer by abuelitovip · Nov 18, 2017 at 12:39 AM
can you test with:
pos.x = target.transform.position.x - ((2f + (1f * randXFactor)) * dir);
test this option for see the result in Debug.Log and use play and pause button for see the log with calm.
Debug.Log( target.transform.position.x - ((2 + (1 * randXFactor)) * dir));
this can help to see the fail in the numbers.
I can't really pause because it crashes, but at the moment of the crash, theres nothing unusual about these numbers.