【问题标题】:How to access an instantiated GameObject from another function in Unity 2D如何从 Unity 2D 中的另一个函数访问实例化的游戏对象
【发布时间】:2022-11-30 03:46:49
【问题描述】:

我只想在另一个函数中使用实例化的 GameObject 但它总是说 当前上下文中不存在名称“newObject”

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Deletenow : MonoBehaviour
{
    public Transform Player;
    public GameObject myObject;
    public Vector2 thisVector;

    void Start()
    {
        thisFunction();
    }

    void Update()
    {
        thisVector = newObject.transform.position;
    }

    public void thisFunction()
    {
        GameObject newObject = Instantiate(myObject, Player.transform.position, Player.rotation);
    }

}

我找不到任何可以帮助我的资源

【问题讨论】:

    标签: unity3d instantiation void


    【解决方案1】:

    您的问题是 newObject 是在函数范围内定义的。将其定义在类声明下方,以便它持续存在。然后删除的GameObject部分

    newObject=Instantiate(myObject,Player.transform.position,Player.rotation);

    该类看起来像public class DeleteNow : MonoBehaviour { GameObject newObject;… }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      相关资源
      最近更新 更多