简体   繁体   English

如何从 Unity 中的另一个脚本访问变量?

[英]How can I access a variable from another script in Unity?

I want to be able to use a variable from one script in another script.我希望能够在另一个脚本中使用一个脚本中的变量。 My goal is to allow me to reference the particleScale variable and use it to affect the size of the object connected to the second script.我的目标是允许我引用particleScale变量并使用它来影响连接到第二个脚本的对象的大小。 I also want to later reference other variables from other scripts.我还想稍后从其他脚本中引用其他变量。 There also will be several instances of each object.每个对象也会有几个实例。 This is my first script;这是我的第一个脚本;

public class Particle : MonoBehaviour
{

    public float particleSize;
    public Transform particle;

    void Start()
    {
        particle.localScale *= particleSize;
    }
}

This is my second;这是我的第二个;

public class Magnetic : MonoBehaviour
{

    public Transform magnetic;

    void Start()
    {
        magnetic.localscale *= Particle.particleSize;
    }
}

Try this:尝试这个:

public class Particle : MonoBehaviour
{
    public static Particle instance;
    public float particleSize;
    public Transform particle;

    void Awake()
    {
      instance = this;
    }
    void Start()
    {
        particle.localScale *= particleSize;
    }
}


public class Magnetic : MonoBehaviour
{
    
    public Transform magnetic;

    void Start()
    {
        magnetic.localscale *= Particle.instance.particleSize;
    }
}

There are a few ways to do it,有几种方法可以做到,

  1. Using the FindObjectOfType()使用FindObjectOfType()

     public class Magnetic : MonoBehaviour { public Transform magnetic; Particle particle void Start() { particle = FindObjectOfType<Particle>(); magnetic.localscale *= particle.particleSize; } }
  2. Using a singleton pattern (only if you are just going to have a single object of type Particle)使用单例模式(仅当您只需要一个 Particle 类型的对象时)

     public class Particle : MonoBehaviour { public static Particle Instance; public float particleSize; public Transform particle; void Awake() { if (Instance != null) { Destroy(gameObject); } else { Instance = this; } } void Start() { particle.localScale *= particleSize; } }

Then the class will globally accessible from Particle.Instance , so you can use any public methods from it or in your case the Particle.Instance.particleSize .然后该类将从Particle.Instance全局访问,因此您可以使用其中的任何公共方法,或者在您的情况下使用Particle.Instance.particleSize

Generally speaking, you don't want your variables to be public.一般来说,你不希望你的变量是公开的。

You really want to have those variables as private and access them through a public method.您确实希望将这些变量设为私有并通过公共方法访问它们。

If you need to access your variables from the Unity Inspector, it is still a better idea to use [SerializeField] :如果您需要从 Unity Inspector 访问您的变量,使用[SerializeField]仍然是一个更好的主意:

public class Particle : MonoBehaviour
{

    private float particleSize;
    [SerializeField] private Transform particle;

    void Start()
    {
        particle.localScale *= particleSize;
    }

    public float GetParticleSize() {
         return particleSize;
    }
}

And the way you call them from a different class could be through the usage of FindObjectOfType<> like others have said or you could create a gameobject with the script attached to it and create a public reference to it in the script of your second gameObject and assign it in the inspector!你从不同的类中调用它们的方式可能是通过使用 FindObjectOfType<> 就像其他人所说的那样,或者你可以创建一个带有脚本的游戏对象,并在你的第二个游戏对象的脚本中创建对它的公共引用和在检查员中分配它!

public class Magnetic : MonoBehaviour
{

    public Transform magnetic;
    public GameObject particleOb;

    void Start()
    {
        magnetic.localscale *= particleOb.GetComponent<Particle>().GetParticleSize;
    }
}

In this last way of doing it you basically assign the particle GameObject in the inspector and grab the script from its components.在最后一种方法中,您基本上在检查器中分配粒子 GameObject 并从其组件中获取脚本。 This method also allows you to have multiple objects with few modifications on each object, but still calling the same methods!此方法还允许您拥有多个对象,对每个对象进行少量修改,但仍调用相同的方法!

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

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