Ive got the following on my gun:
function GetFireCount(){
return shotsFired;
}
But I want to call this function from another script thats on a completely different game object, how can I access it?
Ive got the following on my gun:
function GetFireCount(){
return shotsFired;
}
But I want to call this function from another script thats on a completely different game object, how can I access it?
Try this:
// from the /other/ game object script
var shotsFired = GameObject.Find("GunObject").GetComponent("GunObjectScript").GetFireCount();
It says:
GetFireCount is not a member of UnityEngine.Component
what’s the name of your script attached to the gun object?
and what’s the name of the gun object?
I’ll post code accordingly…
You may also need to make your function ‘public’:
public function GetFireCount(){
return shotsFired;
}
my game object is called shotgun and the script is called Shotgun
bump still cant figure this out
Maybe it’s easier to use a static variable here?
Hey Hatman,
The error you’re getting seems odd because you’re using javascript. You should be able to call it with:
GameObject.Find("shotgun").GetComponent(typeof Shotgun).GetFireCount()
Using C# you call it with:
((Shotgun) GameObject.Find("shotgun").GetComponent(typeof(Shotgun))).GetFireCount();
I doubt you’re using static typing in javascript, but if you are then I think that you would write it the same way that you write it in C#.
Sorry, I missed your earlier post.
If your gun script is attached to your gun object as a component - ie when you select your gun in the hierarchy, and you can see in the inspector that the gunscript is part of the object as a component in the list, then this code should work from the other script:
var shotsFired = GameObject.Find(“shotgun”).GetComponent(“Shotgun”).GetFireCount();
// as mentioned before, your GetFireCount function may need to be declared as public.
Hope that helps…
At every step of this that doesn’t work, you ought to be using print() or Debug.Log() to test areas of the code that might be breaking.
ie.
from the other script (not the shotgun script):
print(GameObject.Find(“shotgun”));
that will tell you if the reference is working, and then:
print(GameObject.Find(“shotgun”).GetComponent(“Shotgun”));
See what that tells you…
Do you have:
#pragma strict
at the top of your Class? If you do, it could be the same bug I am running into…
Just remark that line out and see if it works.
When I add
print(GameObject.Find(“shotgun”).GetComponent(“Shotgun”));
or
print(GameObject.Find(“shotgun”);
Nothing happens.
Also, how do I remove #pragma strict as mentioned?
Ok just ignore my post. I was getting the error by trying to do something similar.
You have to put that “#pragma strict” line in yourself, so if it’s not there, then don’t worry about it.
Assuming everything is working well, you would use:
//‘GunObjName’ is the name of the gun object in the hierarchy view.
//‘GunScript’ is the name of the .js file.
//‘FunctionName’ is the public function in the ‘GunScript’ class you want to call.
GameObject.Find(“GunObjName”).GetComponenet(GunScript).FunctionName();
You don’t use quotes for the script component.
I also tried:
Debug.Log(GameObject.Find(“shotgun”).GetComponent(Shotgun));
and nothing comes out on the console.
Theres no pragma strict in my code so I guess its not there. Any ideas why its not working?
where are you putting the print call?
Do you want to PM me your email address and I’ll send you an empty project with code that works?
Are you sure Shotgun is a component of the gameObject ‘shotgun’ ?
#pragma strict
function Update(){
if(Input.GetButtonDown("Jump")){
Debug.Log(GameObject.Find("Main Camera").GetComponent(Follow));
}
}
Works for me…
Oh yea, I also added an empty game object, and applied the script to it, and it worked. So calling the script from a differnt object…
Ive created a new game object and script and this works:
Debug.Log(GameObject.Find(“shotgunTest”).GetComponent(shotgunTest)));
but
Debug.Log(GameObject.Find(“shotgunTest”).GetComponent(shotgunTest).GetFireCount());
Does not, same problem, GetFireCount is not a member of UnityEngine.Component.
I have that function in my shotgunTest script:
public function GetFireCount(){
return “test”;
}
Ok I think I might know. You have to get the transform of the other object…
#pragma strict
function Update(){
if(Input.GetButtonDown("Jump")){
var otherTransform = GameObject.Find("shotgunTest").transform;
//we now have access to the objects 'components'
var shotgunScript : shotgunTest = otherTransform.GetComponent(shotgunTest);
print(shotgunScript.GetFireCount());
}
}
#pragma strict
public function GetFireCount(){
return "test";
}
EDIT:
Alternatly:
#pragma strict
function Update(){
if(Input.GetButtonDown("Jump")){
var otherScript : shotgunTest= GameObject.Find("shotgunTest").transform.GetComponent(shotgunTest);
//we now have access to the objects 'components'
print(otherScript.GetFireCount());
}
}
bunzaga, superb! It works, cheers for all the help.