简体   繁体   English

脚本中的方法永远不会被调用

[英]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: 提供了这样的方案:

  • We have a GameObject in scene, let's call it Item 我们在场景中有一个GameObject ,我们称之为Item
  • We have ProjectileScript attached to Item 我们在Item附加了ProjectileScript
  • We have a 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.

相关问题 调试问题(方法从未调用) - Problem with debugging (method never called) 错误:从未调用过的方法上的“找不到 RPC 方法” - Error: "RPC method not found" on method that was never called Moq中从未调用过的模拟异步方法 - Mocked async method never called in Moq 接收 POST 请求但从未被调用的操作方法 - Action method receiving POST request but is never called 永远不会调用CustomModelBinder的OnPropertyValidating方法 - OnPropertyValidating method for CustomModelBinder never gets called 永远不会使用ValidatorFactoryBase调用AbstractValidator的Validate方法 - Validate method of AbstractValidator never is called using ValidatorFactoryBase 自定义安装程序类,从未调用回滚方法 - Custom Installer class , rollback method never called 为什么从来没有调用Downloadlistener的OnDownloadStart方法? - Why does the OnDownloadStart Method from Downloadlistener never gets called? 从泛型方法内部调用时,永远不会为变量分配值 - Variables are never assigned a value when called from inside a generic method ILocationListener.OnLocationChanged方法从未在RequestLocationUpdates()之后调用 - ILocationListener.OnLocationChanged Method never called after RequestLocationUpdates()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM