Let me try to explain:> non static member 'GetComponent'
This tells you that GetComponent is not static. In case you forget, a static function belongs the the class it is a member of. All objects created using that class share the static function. A good example in Unity is `GameObject.Find()`. This function searches all game objects looking for one with a particular name. It's a static function. Compare that with `GameObject.GetComponent()`. The `GetComponent` function needs an object of type GameObject so it know which game object to add the component to.
So, in your code, you are calling `GameObject.GetComponent()`. Since `GetComponent` is a non-static member, you need to call it via an object of type `GameObject`. This means, that you should have a variable, perhaps:
var go : GameObject;
so you can then call:
AdjCurShields = go.GetComponent("Shields");
where you have set `go` to the game object which has the Shields script on it. If that game object happens to be the game object with the script on, then you can simply do:
AdjCurShields = GetComponent("Shields");
Unity guesses that you mean the "current" game object. Or you could do:
AdjCurShields = gameObject.GetComponent("Shields");
Since all components have a member called `gameObject` set to the game object the script is attached to.
So, if you are still reading you'll have learned something about static members. How to know if something is static or not? Well, functions documented under the heading "Static Functions" in the script reference are, well, static, so are called using the class name in front of them. Everything else is non-static, and so need an object of the class.
↧