[Unity] C#脚本中如何使用静态属性代表物体

91 阅读1分钟

如图所示

image.png

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class Test : MonoBehaviour
{
    public GameObject Cube;
    public GameObject Prefab;
    // public GameObject 
    // Start is called before the first frame update
    void Start()
    {
        //GameObject go = this.gameObject;
        // Debug.Log(gameObject.name);

        // 添加组件
        //Cube.AddComponent<AudioSource>();
        GameObject User = GameObject.Find("User");
        Debug.Log(User.name);
        User.AddComponent<AudioSource>();

        //通过预设体创建物体 (注意, 需要退出播放模式,再设置好预制体,拖进去Test脚本中的预制体)
        Instantiate(Prefab);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

image.png