2016-11-30 3 views
-2

2 개의 스크립트가 있고 다른 스크립트 트리거를 활성화하는 첫 번째 스크립트에 대해 하나의 트리거를 만들려고합니다. 조사를 시도하지만 여전히 내 코드에 고정되어 있습니다.변수로 onCollitionEnter 사용

내 첫 번째 코드는

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 ... right와 같으면 테스트하지 않습니까? – Alox

+0

예 마지막으로 몇 가지 변경 사항을 작성하고 두 가지 새로운 변수를 사용하고 변경하려면 wen은 0부터 1까지 필요합니다. 코드를 테스트했지만 테스트하지 않은 상태에서 최종 결과를 모두 알게됩니다. 그리고 아니요. 게임 관리자를 사용하지 않았습니다. 저는 다른 코드의 개발자이고, 몇 주 전에 js와 C#으로 시작합니다. –

+0

그리고 그게 내 힘든, 그것을 비교하는 방법을 모르겠다, 그래서 내가 2 개의 새로운 공개 변수 하나 하나를 위해 그것을 결정하고 그것을 원한다면 원한다면 원한다. –

답변

1

당신이 게임 관리자 스크립트를해야합니까입니까? 충돌이 당신의 trigguercubex 클래스에

GameManager.instance.TriggerOccurred(); 

을 감지 할 때 당신이 세터와 게터 엔드 포인트 클래스 에서
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(); 
} 

나는 내 게임에 GameManager 스크립트를 첨부 메인 카메라

+0

안녕하세요 안녕하세요. 그것의 오류가 왔어 GameManager에는 예를 들어 정의가 포함되어 있지 않습니다. –