2017-05-01 2 views
0

私はこの質問が10,000回前に尋ねられたことを知っています。しかし、私はちょうどその答えを把握しているようです。私はUnityでいくつかのプロトタイプのゲームデザインを作成していますが、TONがVisual Studiosの自動補完機能を使って何をしているのかを知っています。
私はプレハブのコードを掴むのに非常に苦労しています。ここに投稿するための簡単なテストのために、私は "Left Control"が押されてリリースされると車をピックアップするマグネットを作ります。問題は、シーンにいくつか落としたときに、磁石がプレハブのランダム版をピックアップすることです。スクリプトに添付された単一のゲームオブジェクトとプレハブをどのように参照できますか?ユニティC#理解できないクローンへの参照prefab

詳細を追加

ここに主要な問題があります。マグネットは、トリガゾーンの内側のピックアップしない任意のランダムなオブジェクト "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; 
     } 

    } 
} 

そして、ここでは、オブジェクトが、私はこれが唯一のCraneMagnetスクリプト を使用して実現するためにどのようにこのインスタンスで考え出した磁石

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 
     } 
    } 
} 
+0

私はその質問を理解していません。あなたは少し明確にできますか? – Hristo

+0

だから私は車でプレハブを作った、それはタグ付きの車です。私は、トリガーコライダーに "Crane"とタグ付けされたボックスを作成しました。objectMovedByTagスクリプトは、インスペクターで "Crane"という名前のgetTagを取得します。しかし、マグネットが車の荷物に載っていれば、それはトリガーコリオーダーのものではなくランダムな車を拾うでしょう。 – Melsy

+0

あなたの質問タイトルは 'インスタンシエート'について尋ねますが、あなたのコードはそれを呼び出すことはありません。質問のタイトルと本文を調べて、実際に何を求めているかを明確にする必要があります。 – Abion47

答えて

0

によってピックアップされていますここに興味のある人は、別のオブジェクトからタグ付き剛体を取り上げるコードです。

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; 
     } 
    } 
} 
+0

説明: 'GameObject.FindGameObjectWithTag'は、そのタグを持つゲームオブジェクトを検索します。複数ある場合、指定されたオブジェクトであるという保証はなく、タグを持っているということだけがあります。これは、この答えが、ゾーンに入ったものと同じタグを持つオブジェクトのシーン全体を検索するのではなく、トリガーゾーンを使用してピックアップすべきもの(トリガーに入ったもの)を特定する理由です。 – Draco18s

関連する問題