简体   繁体   English

如何将Sphere作为参数传递给Unity3d中的脚本?

[英]How to pass Sphere as parameter to script in Unity3d?

I have created several Sphere objects and added them to my scene in Unity3d. 我创建了几个Sphere对象,并将它们添加到Unity3d中的场景中。 Now I need to pass these objects to my script and work with those objects. 现在,我需要将这些对象传递给脚本并使用这些对象。 How do I do that? 我怎么做? For example when I need to pass Terrain object to the script I would write... 例如,当我需要将Terrain对象传递给脚本时,我会写...

public class A: MonoBehaviour {
    public Terrain terrain;
    void Start () {
        terrain = terrain.GetComponent<Terrain>();
    }
    .....
}

... and this would work but I can't write: ...这可以工作,但我不能写:

 public class A: MonoBehaviour {
    public Sphere sphere;
    void Start () {
        sphere = sphere.GetComponent<Sphere>();
    }
    .....
}

because there is no such type as Sphere (right?). 因为没有像Sphere这样的类型(对吗?)。

Use GameObject (which is the type of most of the scene objects) instead of Sphere as your reference type. 使用GameObject(这是大多数场景对象的类型)而不是Sphere作为参考类型。

See also http://docs.unity3d.com/Manual/GameObjects.html on what GameObjects are. 另请参阅http://docs.unity3d.com/Manual/GameObjects.html有关什么是GameObject。

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

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