2016-11-30 32 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不包含例如定义 –

相关问题