I currently have damage script calling a function from another script which is called shields. I’ve fooowed this guys video (Unity3D Tutorial #62 [ Calling Functions from other Scripts ] - YouTube) but when i start the game i get
“(20,36): BCE0020: An instance of type ‘UnityEngine.GameObject’ is required to access non static member ‘GetComponent’.”
Here is the script i am pulling the function from.
var maxShields = 100;
var curShields = 100;
function Start()
{
}
function AdjustCurShields(adjustShields:int)
{
curShields += adjustShields;
if (curShields>maxShields)
curShields=maxShields;
}
function Update()
{
if (curShields<=0)
Debug.Log("shields are down");
AdjustCurShields(0);
}
here is the script with the function i am calling from the other script.
function Start ()
{
}
function Update ()
{
if (Input.GetKeyUp(KeyCode.F))
{
Attack();
}
}
function Attack()
{
var AdjCurShields : Shields;
AdjCurShields = GameObject.GetComponent("Shields");
AdjCurShields.AdjustCurShields(-10);
}
Thanks in advance.
why dont u make a variable to directly refrence the script?Where you see “NameOfScript” put the name of the script that you want to access like
scriptName = GameObject.Find("Name_Of_Game_Obj_Where_script_Is_Attached").GetComponent( NameOfScript);
//call it like
SendMessage("AdjustCurShields",-10);
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.
The problem is at this line of code:
AdjCurShields = GameObject.GetComponent(“Shields”);
Unity need a specific GameObject to run GetComponent function, in this case it’s the GameObject that attached with “Shield” script.