[英]Method in Script is never called
I have a Projectile Script attached to a GameObject. 我有一个附在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.
现在,我想从附加到GameObject A的脚本A中访问Projectile Script中的LinearProjectile()方法。在这里,我在脚本A中创建了Projectile类型的公共字段,并传递了一个附加了Projectile Script的GameObject。
To access LinearProjectile() method i just did. 我刚刚访问了LinearProjectile()方法。
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.
如何从其他GameObject脚本访问此方法。 I tried GetComponent().method() but even though i can't acess what is the error here?
我试过GetComponent()。method()但即使我无法访问这里的错误是什么? i spent alot of time to find out but no success.. Help me out
我花了很多时间找出答案,但没有成功。
Provided a scenario like this: 提供了这样的方案:
GameObject
in scene, let's call it Item
GameObject
,我们称之为Item
ProjectileScript
attached to Item
Item
附加了ProjectileScript
A
script attached to Item
A
连接到脚本Item
All we need to do is to call inside A
script: 我们需要做的就是在
A
脚本中调用:
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: 如果在另一个
GameObject
,我将亲自在脚本A
创建要在Inspector
使用的字段,例如: public GameObject ProjectileScriptHolder
,然后将GameObject
从保存ProjectileScript
场景拖动到Inspector
该变量中,并以这种方式进行访问:
ProjectileScriptHolder.GetComponent<ProjectileScript>().LinearProjectile();
I would also check every GetComponent<T>
before calling method as it might return null, ie: 我还将在调用方法之前检查每个
GetComponent<T>
,因为它可能返回null,即:
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. 如果您无法通过连接项目
Inspector
,你可以使用FindWithTag()
找到GameObject
的场景,只要它具有正确的Tag
附着。 FindWithTag()
takes string
as parameter and will look for GameObject
with such tag in scene. FindWithTag()
采用string
作为参数,并会查找GameObject
与场景这样的标签。
I understand that you want to access a script from another script on another gameobject! 我了解您要从另一个游戏对象上的另一个脚本访问一个脚本!
Way 1 方式1
There is a simple way to do that, caching the script. 有一种简单的方法可以缓存脚本。 (Assuming the name of the script is: "ProjectileScript")
(假设脚本的名称为:“ ProjectileScript”)
On ScriptA you create a 在ScriptA上创建一个
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. 您需要初始化它,有两种方法:1-在GameObjectA的检查器中,将ProjectObject与projectileScript一起拖动。
or 要么
2.1- if the script is in the same game object: 2.1-如果脚本在同一游戏对象中:
pS = GetComponent<ProjectileScript> ();
2.2- if it is on another gameobject you might need to find this object somehow. 2.2-如果它在另一个游戏对象上,则可能需要以某种方式找到该对象。 Consider using a tag
考虑使用标签
pS = GameObject.FindGameObjectWithTag("projectile").GetComponent<"ProjectileScript">();
And cast it inside Script A: 并将其转换为脚本A:
pS.LinearProjectile();
Way 2 方式二
Or you can make a generic tag on the GameObject with the ProjectileScript and find it that way: 或者,您可以使用ProjectileScript在GameObject上制作一个通用标签,然后通过以下方式找到它:
GameObject.FindGameObjectWithTag("projectile").GetComponent<"ProjectileScript">.LinearProjectile();
This make sense for you? 这对您有意义吗? Sorry my english.
对不起,我的英语。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.