简体   繁体   English

在Tree Unity 3D C#中驾驶

[英]Driving in Tree Unity 3D C#

Hi guys i'm trying to drive in the my GameObject (The Player) in my Project, I want a script to navigate to a var in a script that is on another GameObject. 嗨,大家好,我想在我的Project的GameObject(玩家)中开车,我想让一个脚本导航到另一个GameObject上的脚本中的var。 It's not easy because there is Parents and children... Here is my tree: 这并不容易,因为这里有父母和孩子……这是我的树:

>PlayerEntity
    >Canvas
        Gun_Name_Text
        Gun_Ammo_Text
    >Player
        Sprite

I want a script attached to 'Gun_Name_Text' to fetch a var in a script attached to 'Player', so i didnt manage to do it with : 我想要附加到“ Gun_Name_Text”的脚本来获取附加到“ Player”的脚本中的变量,所以我没有设法做到这一点:

var ammo1 = GetComponentInParent<GameObject> ().GetComponent<weapon> ().weaponOn.Ammo1; 

PS: I prefer not to use GameObject.Find() Thanks in advance PS:我不想使用GameObject.Find()预先感谢

As I said in the comments, the easiest way to do this would to be to just assign the variable in the inspector. 正如我在评论中所说,最简单的方法是在检查器中分配变量。 However if you can't do this then you could simply use: 但是,如果您不能执行此操作,则可以使用:

OtherScript otherScript = null;

void Start()
{
    otherScript = transform.root.GetComponentInChildren<OtherScript>();
}

Note: This will set otherScript equal to the first instance of the OtherScript that it finds in the child objects though. 注意:这会将otherScript设置otherScript等于它在子对象中找到的OtherScript的第一个实例。 You will have to use GetComponentsInChildren if you have more than one OtherScript object in the children. 如果GetComponentsInChildren有多个OtherScript对象,则必须使用GetComponentsInChildren

For your specific example you could use: 对于您的特定示例,您可以使用:

var ammo1 = transform.root.GetComponentInChildren<weapon>().Ammo1;

If you are calling this often then it would be wise to cache a refrence to the weapon script though. 如果您经常调用它,那么明智的做法是将对weapon脚本的引用缓存起来。 You will also have to do this if you want to modify the Ammo1 variable that is a member of the weapon class as it is passed to the var ammo1 by value and not by reference. 如果要修改属于weapon类成员的Ammo1变量,因为它是通过值而不是通过引用传递给var ammo1 ,则还必须这样做。

There are cases where my game object does not know everything that interacts with it. 在某些情况下,我的游戏对象并不知道与其交互的所有内容。 An example would be a destructible object. 一个示例是可破坏的对象。 If I wanted to know so what I'm hitting that was destructible I'd have all destructible objects inherit from a base type or interface and search on that type. 如果我想知道我要破坏的是什么,我会让所有可破坏的对象都从基本类型或接口继承并在该类型上进行搜索。 Here is an example: 这是一个例子:

private void CheckForDestructables(Collider2D c)
{
  this.Print("CheckForDestructables Called");
  string attackName = GetCurrentAttackName();
  AttackParams currentAttack = GetCurrentAttack(attackName);
  D.assert(currentAttack != null);
  this.Print("CheckForDestructables Called");

  if (currentAttack.IsAttacking && c.gameObject.tag == "Destructable")
  {
    List<BaseDestructibleScript> s = c.GetComponents<BaseDestructibleScript>().ToList();

    D.assert(s.Any(), "Could Not find child of type BaseDestructibleScript");

    for (int i = 0; i < s.Count(); i++)
    {
      s[i].onCollision(Movement.gameObject);
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM