Resetting Answer object and moving it back – finished in “Resetting game state”

This post became too much of a hot mess, so I put the solution in “Resetting game state” at http://girlcodeitblog.com/2016/04/05/resetting-game-state/.   (#todo – why are urls not turning into links … )

But in the interest of saving my work, here’s my original thoughts on it:

I just moved an object onto a collider, everything else happened great, but it’s not moving itself back when I tell it to.

Tried this first:

transform.position = new Vector3(1.84f, 4.43f, 10f); // this is what I tried first and it didnt work

Why no worky?  Tried this.  Still no worky.

 gameObject.transform.position = new Vector3(1.84f, 4.43f, 10f);

So it turns out that the script on this object that allows it to be dragged and dropped by a mouse was the problem.  It was still moveable, and so it wouldn’t change its transform? Don’t quite understand.  But when I add this, suddenly it works:

            MoveObject otherScript = gameObject.GetComponent<MoveObject>();
//                MoveObject answerScript = GameObject.Find(Answer).AddComponent<MoveObject>();
            Destroy(otherScript);

 gameObject.transform.position = new Vector3(1.84f, 4.43f, 10f);

Now the trick is is make this object invisible again (it’s not supposed to be visible during gameplay, only as an answer), and add the MoveObject script back onto it so that when it becomes visible it can be dragged and dropped into the correct answer bin.

What I’d LIKE to do is deactivate it like this:

gameObject.SetActive(false);

However, when I do that, the rest of the game stops working, even when I set it back to active in the other script Start().  Sooo I’ll leave that for another day and just keep transforming the scale to Vector3(0,0,0).  Feels like a hack, but it sure works! (Got it from : http://answers.unity3d.com/questions/7776/how-to-make-an-gameobject-invisible-and-disappeare.html . Want to further explore the pros and cons of each option, but in the meantime I’m using some mix of this and SetActive(false), depending on which one works easily in context.)

gameObject.transform.localScale = new Vector3(0, 0, 0);

Now I just have to add my moveable script back onto the object after it’s been de-scaled.  Note – I tried to see if I could get away without deleting the script because I’d transformed the position and also the scale – turns out it puts it back in the spot I left it in?  Don’t know why.  Actually instead of messing with the MoveObject script, I’m going to see if setting the transform.position in the other script that calls it in its Start() function works.  Seems … cleaner somehow.

GameObject.Find(Answer).transform.position = new Vector3(1.84f, 4.43f, 10f); 

Gosh no that doesnt’ work at all.  Apparently I can’t descale that object while MoveObject script is on it.  Okedoke.  Someday I’ll understand why but in the meantime I’ll make it work.

Now I’ve got this:

 

            MoveObject otherScript = gameObject.GetComponent<MoveObject>();
////                MoveObject answerScript = GameObject.Find(“Answer”).AddComponent<MoveObject>();
            Destroy(otherScript);
//
            // move Answer back to original transform

            gameObject.transform.localScale = new Vector3(0, 0, 0);
            gameObject.transform.position = new Vector3(1.84f, 4.43f, 10f); // this is what I tried first and it didnt work
//            gameObject.SetActive(false);
            // destroyed audio sources  easy peasy 
//            MoveObject otherScript = AssetDatabase.LoadAssetAtPath(Assets/Scripts/MoveObject, typeof(MoveObject)); // apparently I don’t need this part because it’s already been defined, even tho it just got destroyed …
            otherScript = gameObject.AddComponent<MoveObject>();

Okay. So now the Answer object isn’t reappearing.  Messed sthing up somewhere.  It works for half of my objects but not the other half.  This doesn’t make much sense so I must’ve really done sthing fun in there.

I didn’t set it to the right z location, it was behind the background.

 

WRONG:

GameObject.Find(Answer).transform.localScale = new Vector3(0.08f, 0.08f, 0);

 

RIGHT:

GameObject.Find(Answer).transform.localScale = new Vector3(0.08f, 0.08f, 10);

And also apparently it’s not always picking up on the audio components of other objects.  It works for some, but not others.  I’m going to walk away from this soon and let everything marinate.  I think I’ll be able to solve it tomorrow.

Leave a Reply

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