Unity C#-在按钮上单击将游戏对象传递给另一个C#脚本

Unity C#-在按钮上单击将游戏对象传递给另一个C#脚本,c#,unity3d,gameobject,C#,Unity3d,Gameobject,我有多个按钮,上面有不同的游戏对象。 当点击按钮时,我想将游戏对象传递给另一个C#脚本,在某些条件下,该脚本将实例化传递的游戏对象。 我有以下按钮代码: using System.Collections; using System.Collections.Generic; using System.Diagnostics; using UnityEngine; using UnityEngine.UI; public class Element : MonoBehaviour { pr

我有多个按钮,上面有不同的游戏对象。 当点击按钮时,我想将游戏对象传递给另一个C#脚本,在某些条件下,该脚本将实例化传递的游戏对象。 我有以下按钮代码:

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.UI;

public class Element : MonoBehaviour
{
    private Button btn;
    public GameObject furniture;
    // Start is called before the first frame update
    void Start()
    {
        btn = GetComponent<Button>();
        btn.onClick.AddListener(PassObjectToAnotherScript);
    }

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

    void PassObjectToAnotherScript()
    {
        //Code to pass the object to another C# script
    }
}


它可以简单到让第二个脚本公开可以将对象传递给的字段或属性。实现此目的的多种方法之一可能如下所示:

public class Element : MonoBehaviour
{
    private Button btn;
    public GameObject furniture;
    public Receiver recevier;

    void Start ( )
    {
        btn = GetComponent<Button> ( );
        btn.onClick.AddListener ( PassObjectToAnotherScript );
    }

    void PassObjectToAnotherScript ( )
    {
        //Code to pass the object to another C# script
        recevier.PassedGameObject = furniture;
    }
}

public class Receiver : MonoBehaviour
{
    private GameObject _PassedGameObject;
    public GameObject PassedGameObject
    {
        get => _PassedGameObject;
        set
        {
            _PassedGameObject = value;
            Debug.Log ( $"Receiver[{name}] just received \'{_PassedGameObject.name}\'" );
        }
    }
}
公共类元素:单一行为
{
专用按钮btn;
公共游戏物品家具;
公共接管人;
无效开始()
{
btn=GetComponent();
btn.onClick.AddListener(将对象传递到另一个脚本);
}
void passobjecttootherscript()
{
//将对象传递给另一个C#脚本的代码
receivier.PassedGameObject=家具;
}
}
公共类接收者:单一行为
{
私有游戏对象_PassedGameObject;
公共游戏对象PassedGameObject
{
get=>\u PassedGameObject;
设置
{
_PassedGameObject=值;
Log($“Receiver[{name}]刚刚收到\'{{u PassedGameObject.name}\'”;
}
}
}

Milan感谢您的回答,但我在控制台中发现了以下错误:NullReferenceException:对象引用未设置为对象元素的实例。PassObjectToAnotherScript()(位于Assets/Scripts/Element.cs:21)@Hamza您是否将接收器组件添加到对象中,然后将该对象拖到“元素”中的“接收器”字段中组件?非常感谢。我已经做了你回答我的事情,而且成功了。我非常感激。
public class Element : MonoBehaviour
{
    private Button btn;
    public GameObject furniture;
    public Receiver recevier;

    void Start ( )
    {
        btn = GetComponent<Button> ( );
        btn.onClick.AddListener ( PassObjectToAnotherScript );
    }

    void PassObjectToAnotherScript ( )
    {
        //Code to pass the object to another C# script
        recevier.PassedGameObject = furniture;
    }
}

public class Receiver : MonoBehaviour
{
    private GameObject _PassedGameObject;
    public GameObject PassedGameObject
    {
        get => _PassedGameObject;
        set
        {
            _PassedGameObject = value;
            Debug.Log ( $"Receiver[{name}] just received \'{_PassedGameObject.name}\'" );
        }
    }
}