I've searched a lot using google and forum searcher, but I didn't find answer for my problem.
My problem :
Why does this
or this codeCode:
gives me a context related error? For the first code he can't find monster and for the second : controller.Code:
Why? I tried also using Awake() instead of Start(), but keep getting the same error.
Because it's telling you the truth. Those variables are not in the context that they're being called. The variable "monster" is defined inside the Start() method, so it can only be used inside the Start() method. Once Start() returns, the "monster" variable is thrown away, its memory is deleted. If you want "monster" to be available in the Update() method, then you need to define the variable inside the class context. In other words, you need to make it global, like this...
Last edited by Brian Stone; 09-08-2012 at 05:05 PM.
Thank you very much for your answer.
It solved my problem and there's no need for a monster initialization in start(), just drag object in inspector to the script and everything is fine.
Without dragging there was a error
The variable monster of 'speed' has not been assigned.
Sorry for missing that. That error pops up because the compiler doesn't know how Unity's engine works, or how any program works, for that matter. It doesn't know that Start() is guaranteed to be called before the Update() method, but it sees the possibility that Update() is called before "monster" is initialized.The variable monster of 'speed' has not been assigned.
To avoid that error, you simply need to initialize the variable to something. In this case, you should set the variable to "null", because GameObject is a reference type. Alternatively, because this class inherits from MonoBehavior and you've attached it to a GameObject in the Unity editor, you can set the variable in the inspector. Unity generates the initialization code for the variable automatically prior to sending the program to Mono to be compiled.
- GameObject monster = null;