Unity C#理解对克隆的引用而不是预设

Unity C#理解对克隆的引用而不是预设,c#,unity3d,C#,Unity3d,所以我知道这个问题以前被问过一万次。但我似乎无法理解答案。我已经在Unity中构建了一些原型游戏设计了一段时间,并且通过使用Visual Studio auto complete了解了很多东西的功能。 所以我很难掌握预制代码的东西。为了在这里发布一个快速测试,我制作了一个磁铁,当按下“左控制”并释放时,它会拾取一辆汽车。问题是,当我在场景中扔下一些东西时,磁铁会拾取一个随机版本的预制件。如何引用附加到脚本的单个游戏对象与预置 为清晰起见,添加 这就是主要问题。磁铁将拾取任何标记为“Car”的随机

所以我知道这个问题以前被问过一万次。但我似乎无法理解答案。我已经在Unity中构建了一些原型游戏设计了一段时间,并且通过使用Visual Studio auto complete了解了很多东西的功能。
所以我很难掌握预制代码的东西。为了在这里发布一个快速测试,我制作了一个磁铁,当按下“左控制”并释放时,它会拾取一辆汽车。问题是,当我在场景中扔下一些东西时,磁铁会拾取一个随机版本的预制件。如何引用附加到脚本的单个游戏对象与预置

为清晰起见,添加

这就是主要问题。磁铁将拾取任何标记为“Car”的随机对象,但不会拾取触发区内的对象。 我怎么能让它只捡起扳机里的“车”

这是磁铁代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//This Goes on the Object that is to be moved by a magnet
public class CraneMagnet : MonoBehaviour
{

    private CraneTrigger craneTrigger;
    public string getTag = "";
    void Start()
    {
        craneTrigger = FindObjectOfType<CraneTrigger>();
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == getTag)
        {
            craneTrigger.canPickup = true;
        }

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
//这发生在被磁铁移动的物体上
公共级CraneMagnet:单一行为
{
私人起重机;
公共字符串getTag=“”;
void Start()
{
craneTrigger=FindObjectOfType();
}
无效对撞机(对撞机其他)
{
if(other.tag==getTag)
{
craneTrigger.canPickup=true;
}
}
}
这是磁铁拾取的物体

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

public class TESTObjectMovedByTag : MonoBehaviour {
    public bool liftObject = false;
    Rigidbody objectRigidbody;

    // strings for get objects of type to declare in the inspector;
    public string getTag = "";

    void Start()
    {
        objectRigidbody = GetComponent<Rigidbody>();
    }

    void Update()
    {
        PickUpObject();
    }

    void PickUpObject()
    {
        if (liftObject)
        {
            objectRigidbody.isKinematic = true;// so the gravity will affect the object
            transform.parent = GameObject.FindGameObjectWithTag(getTag).transform;// make the object child of the magnet 
        }

        if(!liftObject)
        {
            objectRigidbody.isKinematic = false;// so gravity will not affect the object
            transform.parent = null;// remove the magnet parent
        }
     }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类TESTObjectMovedByTag:MonoBehavior{
公共bool-liftObject=false;
刚体对象刚体;
//用于获取要在检查器中声明的类型的对象的字符串;
公共字符串getTag=“”;
void Start()
{
objectRigidbody=GetComponent();
}
无效更新()
{
PickUpObject();
}
void PickUpObject()
{
if(liftObject)
{
objectRigidbody.IsKinetic=true;//因此重力会影响对象
transform.parent=GameObject.FindGameObjectWithTag(getTag).transform;//使对象成为磁铁的子对象
}
如果(!liftObject)
{
objectRigidbody.IsKinetic=false;//因此重力不会影响对象
transform.parent=null;//删除磁铁父项
}
}
}

在本例中,我只使用CraneMagnet脚本就知道了如何实现这一点 对于任何感兴趣的人来说,这里都有一个代码,可以从不同的对象中提取带标记的刚体

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

public class CraneMagnet : MonoBehaviour
    {
    private CraneTrigger craneTrigger;// This is calling only for controls
    public string getTag = "";// Define the tag in the inpector
    private Rigidbody objectPickedUp;
    [HideInInspector] public bool canLift = false;
    [HideInInspector] private bool isLifted = false;// only exists so the objectPickedUp cant be called until is exists 
    void Start()
    {
        craneTrigger = FindObjectOfType<CraneTrigger>();
    }

    void Update()
    {
        //call PickUpObject() in the control script
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == getTag)
        {
            objectPickedUp = other.attachedRigidbody;// make the triggered RididBody become objectPIckedUP 
            craneTrigger.canPickup = true;
        }

    }
    public void PickUpObject()
    {
        if (canLift)
        {
            objectPickedUp.transform.parent = transform;// object becomes child of magnet 
            isLifted = true;
            objectPickedUp.isKinematic = true;// so object can be moved easier
        }
        if (!canLift && isLifted)
        {
            objectPickedUp.isKinematic = false;// so gravity will take over the fall
            objectPickedUp.transform.parent = null;//the picked up object looses magnet parent
            isLifted = false;
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共级CraneMagnet:单一行为
{
private CraneTrigger CraneTrigger;//这只调用控件
公共字符串getTag=“”;//在Inputer中定义标记
私有刚体对象pickedup;
[HideInInstitt]公共bool canLift=false;
[HideInInstit]private bool isLifted=false;//仅存在,因此在存在之前无法调用objectPickedUp
void Start()
{
craneTrigger=FindObjectOfType();
}
无效更新()
{
//在控制脚本中调用PickUpObject()
}
无效对撞机(对撞机其他)
{
if(other.gameObject.tag==getTag)
{
objectPickedUp=other.attachedRigidbody;//使触发的RididBody成为objectPickedUp
craneTrigger.canPickup=true;
}
}
public void PickUpObject()
{
如果(可以提升)
{
ObjectPickeUp.transform.parent=transform;//对象成为磁铁的子对象
isLifted=真;
objectPickedUp.IsKinetic=true;//因此可以更轻松地移动对象
}
如果(!canLift&&IsLift)
{
objectPickedUp.IsKinetic=false;//因此重力将接管下落
objectPickedUp.transform.parent=null;//拾取的对象松开父对象
孤岛=假;
}
}
}

我不明白这个问题。你能澄清一下吗?当然可以。所以我创建了一个带有汽车的预制件,它被标记为汽车。我创建了一个带有标记为“Crane”的触发器碰撞器的框。objectMovedByTag脚本捕获了一个getTag,它在inspector中被命名为“Crane”。但是,如果磁铁位于任何标记了汽车的物品上,它将随机拾取一辆汽车,而不是触发器列下的汽车。您的问题标题询问了
实例化
,但您的代码从未调用过它。你需要在问题的标题和主体上下功夫,以便更清楚地知道你实际上在问什么。标题已更改。问题还不清楚吗?我应该全部重写吗?我还是不明白你的问题。你想知道哪辆车被捡到了吗?解释:
GameObject。FindGameObjectWithTag
将找到带有该标记的游戏对象。如果有多个对象,则不能保证它是任何给定的对象,只能保证它确实具有标记。这就是为什么这个答案使用触发器区域来确定它应该拾取哪个对象(进入触发器的对象),而不是在整个场景中搜索与进入区域的对象具有相同标记的任何对象。