2016-11-30 29 views
-2

我有2個腳本,我嘗試做一個觸發器的第一個腳本來啓用其他腳本觸發器,我試圖調查,但我仍然堅持我的代碼。啓用onCollition輸入一個變量

我的第一個代碼是

using UnityEngine; 
using System.Collections; 

public class endpoint10 : MonoBehaviour { 
public static int IsColliderEnabled; 
endpoint10.IsColliderEnabled = 0; 
void OnTriggerEnter(Collider other) 
{ 
    if (IsColliderEnabled = 1) { 
     //do stuff here 
     // The switch statement checks what tag the other gameobject is,  and reacts accordingly. 
     switch (other.gameObject.tag) { 
     case "end": 
      Debug.Log (other.gameObject.tag); 

      PlayerPrefs.SetInt ("moneyPref", scoreManager.money); 
      PlayerPrefs.SetInt ("scorePref", scoreManager.score); 
      ScoreSystem.level += 1; 
      PlayerPrefs.SetInt ("levelPref", ScoreSystem.level); 
      Debug.Log ("values stored"); 
      Application.LoadLevel ("level_11"); 
      break; 

     } 
    } 
    // Finally, this line destroys the gameObject the player collided with. 
    //Destroy(other.gameObject); 
} 

}

和我的第二個代碼

using UnityEngine; 
using System.Collections; 

public class trigguercubex : MonoBehaviour { 

public GameObject[] objects; 

void OnTriggerEnter(Collider other) 
{ 
    endpoint10.IsColliderEnabled = 1; 
    Debug.Log (other.gameObject.tag); 



} 

    // Finally, this line destroys the gameObject the player collided with. 
    //Destroy(other.gameObject); 

}

+1

你確實意識到t在你的if語句中,你將IsColliderEnabled的值設置爲1,而不是測試它是否等於1 ...正確? – Alox

+0

是finaly我已經做了一些改變,並使用兩個更多的新變量,並改變他們我需要0到一個我已經測試代碼withh沒有erros,但我沒有測試,但最終的結果,我會讓所有人知道它的最後。不,我沒有使用過遊戲管理器,我是其他代碼的開發人員,我幾個星期前就開始使用js和c#。 –

+0

這就是我的困難,我不知道如何比較它,所以我決定讓2個新的公共變量一對一關閉和玩它我需要 –

回答

1

你有一個遊戲管理員腳本? 在trigguercubex類檢測

GameManager.instance.TriggerOccurred(); 

碰撞時,你可以使用getter和setter方法

using UnityEngine; 
using System.Collections; 

public class GameManager : MonoBehaviour { 

public static GameManager instance = null; 

private bool triggerredOccurred = false; 

public bool IsTriggerredOccurred { 
    get { return triggerredOccurred;} 
} 

public void TriggerredOccurred() { 
    triggerredOccurred = true; 
} 

void Awake(){ 
    if (instance == null) { //check if an instance of Game Manager is created 
     instance = this; //if not create one 
    } else if (instance != this) { 
     Destroy(gameObject); //if already exists destroy the new one trying to be created 
    } 

    DontDestroyOnLoad(gameObject); //Unity function allows a game object to persist between scenes 
} 

// Use this for initialization 
void Start() { 

} 

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

    } 
} 
在你的端點類

if (GameManager.instance.IsTriggerOccurred) { 
    do some stuff(); 
} 

我附上游戲管理腳本到我的遊戲主攝像頭

+0

嗨你好坦克你的幫助,但它來了一個錯誤GameManager不包含例如定義 –

相關問題