Resetting game state

In the game I’m making, every time a collision occurs, the game state resets to what it was originally – except without that object.  I realized that the problems in yesterday’s post on resetting the Answer object (http://girlcodeitblog.com/2016/04/04/resetting-answer-object-and-moving-it-back-in-progress/) was actually due to me relying on the Start() function to … start again?  For some reason?  It’s actually pretty cool, because I just had a conversation with E (my person, who happens to be a computer scientist) about the things I’ve been learning this week, and it tied in perfectly when I came back to fix my code this morning.  I used to use Python to do procedural things for linguistics research- take in a .csv file, do stuff to it in linear order, spit it back out again.  Same with R.  I never needed to think about functions on objects, just functions as easier ways of grouping together linear actions.  Because of this mental framework, I’d been unconsciously thinking of Start() as something that got looped back to after everything else ran.  Even though I knew consciously that wasn’t how it worked!

So I went through several different ways of resetting the game.  I tried to do it at the end of one of my object’s scripts, but for some reason that wasn’t working.  I tried putting Start() back into the function, which kind of worked, but not fully, because it was the wrong Start(). Oh wait – since all these objects exist at the beginning of the game, all of their Start()s happen at the same time.  Eesh this is confusing.  I’m going to go see if I can put the stuff I care about in the other script’s Start(), then recurse it, and see if that make this simpler than the other stuff I was trying.

Oh wait.  Silly to recurse Start().  Better just to make a function called PlaceAnswer() in the script I care about, and then call it.  I think the problem’s been that I’m not calling it after a “yield return new WaitforSeconds(#ofseconds)”.  Don’t fully understand coroutines yet, because the way I’m used to thinking is step-by-step python, so this is a fun brain stretch.  Okay yay it works!  That saves me some hassle and some bad hacks!

The other thing I was trying to do, that I didn’t keep in my final script, was access a function from a different script.  I got it to work this way :

(“PlayerCoroutine” is the name of the script, and that script has a function called PlaceAnswer() ) :

[used answers on http://answers.unity3d.com/questions/7555/how-do-i-call-a-function-in-another-gameobjects-sc.html]

At the top:

public PlayerCoroutine playercoroutine;

Later:

playercoroutine.PlaceAnswer();

So the reason this ended up not working for me permanently was that I actually delete objects throughout the game, and this required me putting an object with PlayerCoroutine attached to it into the public GUI slot for the script trying to reference it.  First of all, this felt annoying and hacky.  Second of all, that object gets deleted.  So I *could* put it on an object that doesn’t ever get deleted, but gosh that feels a ratsnest of code.

Leave a Reply

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