Reputation: 49
I have a Projectile Script attached to a GameObject. Now i want to access the LinearProjectile() method in Projectile Script from Script A attached to GameObject A. Here i made a public field of type Projectile in Script A and passed in one GameObject where Projectile Script is attached.
To access LinearProjectile() method i just did.
projectile.LinearProjectile(transform.position, transform.rotation, this.direction, this.shotPower);
//Here is my method defination..
public void LinearProjectile(Vector3 position, Quaternion rotation, Vector2 direction, float force)
{
Debug.Log("called");
this.direction = direction;
this.force = force;
this.projectileType = ProjectileType.Linear;
}
All the field value assigned in this method are in default state since this method is never called. How can I access this method from other GameObject Script. I tried GetComponent().method() but even though i can't acess what is the error here? i spent alot of time to find out but no success.. Help me out
Upvotes: 1
Views: 78
Reputation: 843
I understand that you want to access a script from another script on another gameobject!
Way 1
There is a simple way to do that, caching the script. (Assuming the name of the script is: "ProjectileScript")
On ScriptA you create a
public ProjectileScript pS;
and you need to initialise it, there is two ways: 1- on the inspector of GameObjectA, drag the GameObject with the projectileScript.
or
2.1- if the script is in the same game object:
pS = GetComponent<ProjectileScript> ();
2.2- if it is on another gameobject you might need to find this object somehow. Consider using a tag
pS = GameObject.FindGameObjectWithTag("projectile").GetComponent<"ProjectileScript">();
And cast it inside Script A:
pS.LinearProjectile();
Way 2
Or you can make a generic tag on the GameObject with the ProjectileScript and find it that way:
GameObject.FindGameObjectWithTag("projectile").GetComponent<"ProjectileScript">.LinearProjectile();
This make sense for you? Sorry my english.
Upvotes: 0
Reputation: 3082
Provided a scenario like this:
GameObject
in scene, let's call it Item
ProjectileScript
attached to Item
A
script attached to Item
All we need to do is to call inside A
script:
gameObject.GetComponent<ProjectileScript>().LinearProjectile();
If it's on another GameObject
, I would personally create field in script A
to be used in Inspector
, for example: public GameObject ProjectileScriptHolder
and then just drag GameObject
from scene that holds ProjectileScript
into that variable in Inspector
and the access it that way:
ProjectileScriptHolder.GetComponent<ProjectileScript>().LinearProjectile();
I would also check every GetComponent<T>
before calling method as it might return null, ie:
ProjectileScript script = ProjectileScriptHolder.GetComponent<ProjectileScript>();
if (script != null)
script.LinearProjectile();
If you can't attach item through Inspector
, you can use FindWithTag()
and find GameObject
in scene provided it has proper Tag
attached. FindWithTag()
takes string
as parameter and will look for GameObject
with such tag in scene.
Upvotes: 2