Reset position of object

I’ve been lazy and resetting an object’s position to its start position manually with specific Vector3’s.  This gets annoying, though, because if I want to shift my layout, I also have to change multiple parts of my script.  It’d be way better if it did it automatically!

Super easy.  Make a new Vector3, it’ll get the position of the object that the script is attached to.

I had to make mine global so that multiple functions could access it.   I’m a little worried I’m making too many global variables, since I know that’s bad practice in general programming, but I’ll leave that to check out more later (#todo).

private Vector3 answerPosition; // make global variable 

Vector3 answerPosition = transform.position;  // get original location in Start()

answer.transform.position = answerPosition; // put this when you want it to move back to the original position 

The hacky thing is I have to put this in both of my scripts that are moving the object around.  There has to be a more concise way to do this, but I’ll figure that out next time round (#todo).

Gosh now it’s sending the object everywhere.  Hooboy.  Something’s gone very wrong!

Come to think of it, it’d be helpful to know how Debug.Log works. I’ll do that right now instead.  Okay. So click collapse on the console, and then click the speech bubble with the exclamation point in it – leftmost of the top right tabs in the console. (http://answers.unity3d.com/questions/757659/console-not-outputting-from-debuglog.html & http://answers.unity3d.com/questions/563309/i-cant-get-debuglog-to-work-at-all.html).

Debug.Log(answerPosition);

However, it’s only outputting this: UnityEngine.Debug:Log(Object) .

Oh now I feel silly!  There are two lines in each message (three if you click on it and look at the bottom).  The top line is the Vector3 I requested.  Got it.  Now I can see that my Vector3s are going all over the place, even just during the Start() function.  Let’s figure out what’s doing that …

There are apparently 4 different Vector3s that are happening here.  And Debug.Log() doesn’t seem to be working when I put it in again at the end of the coroutine.

Ah. Fixed it. Accidentally set it to “transform.position” instead of “answer.transform.position” when it was attached to a different game object, so it was sending it to random other game objects’ first locations.  Crazy!  Looks like I don’t need it to happen in both scripts, only the one attached to Answer.  And this also helped me realize that the other script shouldn’t be the one that makes Answer invisible in Start(), but instead to put that on the script attached to Answer.  Baby steps toward making this better! (At first this looked like it messed up my colliders, but after checking things, I restarted the program, and now it works.   ¯\_(ツ)_/¯  )

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *