A Few Questions about scripting approach.

For the last several months I have been scripting in Unity, with the exception of a harddrive dieing and losing several weeks of work, I’ve gotten everything back up and running for the most part.

However, now I am suddenly getting the oddest of occurances that I can only explain as when 1 + 1 should = 2, its equaling 10.

Unfortunatly posting the slew of scripts up here isnt a good idea, and I have many scripts that tie together. As in more than 10 pages of scripts.
While I realized rather late in the game it was foolish to not simply build a message routing system, I sort of
have created one and I am currently implementing one.

Here’s the questions that I have from the odd things I am seeing:

  1. Currently I am enabling and disabling scripts, quite frequently, because I only need them to run once, but I need them to run once and during a particular game state. I am noticing that by doing this, when I run the game it appears to be creating hidden instances of the scripts when they are enabled thus doubling, tripling, quadrupoling the script’s output.
    Is this a common occurance?

  2. I have noticed that during disabling and enabling scripts, the OnEnable(), Awake(), and Start() functions are not reliable. In fact, of the three, I still have to manually call Start() even after re-enabling the script, other wise start doesnt run. I havent had any success getting OnEnable() or Awake() to work. Are these other two specific to the time of the start of the game itself? (Sort of a compound question, sorry)

  3. I have a non-loop function, that loops. It loops every frame, I have to guess that something, somewhere, is calling to it from an Update(). However I am not calling to it from an Update(). I am wondering if this is some random error? The script worked literally for months, now its suddenly looping every frame, even if I force it to run once.
    The closest thing is a clause I have in an Update on a script that is disabled by the latter script, then re-enabled if a specific clause is met. It ‘appears’ that it is ignoring the clause, enabling the Update() of the disabled script, thus looping itself somehow.
    Yea sorry this paragraph is kinda a brain spew.

  4. Alot of these problems, I am hoping will be resolved with implementing a Message Router (nice one on the wiki) and using it to establish game states. Then I am hoping to simply say “If game state is this, then you can do this”.
    The question here is if a message router will help me to establish these game states. Unfortunatly without a routing system right now, I have to Find gameobjects alot and check and see what state they are in. From what I understand doing this too much is a no-no, and I’m doing it ALOT. heh.

  5. I have an issue where currently when the player presses a specific button, it is to place an object under thier control, then check if its allowed (based on game current state) to spawn another object for placement. Even if the user taps the key like Speedy Gonzalas, 3 - 5 more objects are instantly spawned and placed. Is there a good reference for pointing someone on how to control input so it doesnt run super-fast in nanoseconds, or should I just tell it to yeild for a few seconds to slow it down?

Thanks for your input/suggestions.

I can’t answer all your questions (at least not without further information), but it sounds like at least some of the problems you describe are due to logical errors or other problems with the code or how the project is set up.

Regarding Start(), Awake(), and so on, a good first step might be to read this (if you haven’t already), just to make sure you correctly understand when the various functions you mentioned are supposed to be called.

It’s always possible you’ve run into a bug into Unity, but I think program logic errors may be the more likely culprit here.

The system you describe sounds fairly complex, and it’s hard to say purely based on your description of it what the problem(s) might be. One approach you could take though would be to create one or more minimal example projects demonstrating the problems in question; for example, you could create a single, simple project that demonstrates Start(), Awake(), or OnEnable() not being called when it should be (or vice versa). Then, you could upload the project here for people to check out, and/or file a bug report if it seems clear that the behavior is incorrect.

Thats a good idea and I actually got started today with that in mind, to pull out pieces and attempt to run them by themselves.

The problem I am having is that it is really hard to troubleshoot what is wrong because I dont have an overall manager for the state of things.

At the same time I am adding such a manager, and peice by peice rebuilding.
Thank you for that link, all I have read so far is just the stuff in the script reference.

You aren’t really enabling/disabling scripts per se, you are enabling/disabling custom Monobehaviours. If you only need something to happen once then it does not make sense to use a separate Monobehaviour at all. In place of enabling the Monobehaviour you just call a method instead. You can create classes which are not derived from Monobehaviour and thus objects of said class can be created as you would create any other variable. Only use Monobehaviour derived classes for things which you will attach to a GameObject and will make use of Update, or FIxedUpdate or other game messages.

They are reliable, you are just confused as to what they are supposed to do. Jesse posted a link explaining this.

The method is being continuously called, probably from Update or possibly OnEnable if you are enabling it every-frame. You need to explain where exactly this method is called.

I haven’t com across the need for such a system but I guess it depends what kinds of games you are making. In the real world things are autonomous agents which only react to the information they have available to them. Whenever possible I try to structure code this way such that entities do not rely on a God object to instruct them how to act. I dunno if that’s helpful at all…

I’ve never had this problem before but it could be related to how you are getting the Input. What method are you using to determine a key-press?

Yea I’m just now starting to realise this, unfortunatly I don’t come from a professional programming training background, while I’ve been an assistant programmer and hilariously a lead programmer professionally, ive never had any training in it.
However each of these scripts do need to be on specific game objects, I just need them to run during specific game states.

Yes indeed, I was utterly confused. I have them working correctly now. OnEnable has solved alot of my issues infact. I am unsure why it wasn’t working before, must have been another error blocking the thread.

It is called in an Update(), however only when that script that calls to it, is enabled, and only if 2 cases match, AND 2 other cases match. It was looping anyway even when the cases didnt match. (Tested this with debuglog reporting the cases.)
What stopped it, was by adding a yield. Literally.

In this case I have learned alot about the Coroutine system inherent to Unity. There’s several manager/statemachine/schedulers on the wiki written by some nice folks.

http://www.unifycommunity.com/wiki/index.php?title=Scripts

Also ran across this nice blog:
http://www.cpudreams.com/2010/02/coroutines.html

I have realised that this will help alot for me to troubleshoot errors in the future, as it stands right now about 10 different scripts are calling and cross checking states with each other and it is compounding the ability to troubleshoot where
an error is placed.

Well I have spent a good portion of the day putting yields all over the place, which has severely helped. I am using Input.GetButton, the way I have it working now is when that happens, I run a function that freezes the object in place by destroying the very script that allows it to be moved about by the player. At the same time, I need to tell another Behaviour to go about spawning the next object (random prefabs in an array) for the player to move about and place.
I added a yeild WaitForSeconds(1); at the top of the next spawn, and that has resolved the spamming of spawning.
Its not perfect, but hey its working.

I really appreciate the replies and thoughts, really helped me clear my head guys and learn to get on the right path.

Thank you!
:razz:

The qouted text below from this thread:
http://forum.unity3d.com/threads/3941-2D-Tutorial-Question-1-Jumping-once-per-button-press.
Resolves the issues I am having with GetButton.

I haven’t had any problem using GetButtonDown (which really could be called “OnButtonDown”, because it only fires once when the button is pressed). GetButton just checks to see if you the button IS down (meaning if you hold the button it will fire every-frame). I guess there is/was some issue with GetButtonDown… but then again that thread is 4 years old… You should be using GetButtonDown when you just want to detect a button press, and not whether or not the button is held-down.

Well it’s essentially impossible for the code to run through an if(false) statement. Whenever you think this is happening, it isn’t. There is something else going on. To test it Debug.Log all of the values of the cases right before the method fires.