2016-09-04 2 views
0

개체를 인스턴스화하려고 시도하고 인스턴스화 할 때 uiManager 변수의 개체를 uiManagerspawnPlayer의 변수로 인스턴스화하려고합니다.개체를 인스턴스화하고 변수를 설정하면 개체가 단일체로 작동하지 않습니다. C#

게임을하고 일시 중지 한 다음 인스턴스화 된 개체를 보면 변수가 설정되지 않습니다.

코드가 있습니다.

using UnityEngine; 
using System.Collections; 

public class playerSpawner : MonoBehaviour { 

    public GameObject[] cars; 
    public uiManager ui; 

    int carSpawned; 

    void Start() 
    { 
     spawn(); 
    } 

    void spawn() 
    { 
     Instantiate (cars [carPicController.next], transform.position, transform.rotation); 
     carPicController.next = carSpawned; 
     Debug.Log ("player spawned"); 
     setuiManager(); 
    } 

    void setuiManager() 
    { 
     //get the thing component on your instantiated object 
     uiManager ui = cars [carSpawned].GetComponent<uiManager>(); 

     //set a member variable (must be PUBLIC) 
     ui = ui; 
    } 
} 
+0

그냥 표준적인 일로, 내 모든 공용 메서드와 변수는 대문자로 시작하고 모든 개인 메서드와 변수는 소문자로 시작합니다. 앤드류 셰퍼드 (Andrew Shepherd)가 코드 밑에서 지적한 것처럼, 기본적으로 'ui'변수를 자체 변수로 설정하고 있습니다. 클래스'ui' 변수를'Ui'로 정의하면 이것을 발견 할 수 있고 또한 this.ui의 추가 문자를'Ui'로 제거합니다 – Canvas

답변

2

문제가 있습니다.

이 작동합니다 :

void setuiManager() 
{ 
    //get the thing component on your instantiated object 
    uiManager ui = cars [carSpawned].GetComponent<uiManager>(); 

    //set a member variable (must be PUBLIC) 
    this.ui = ui; 
} 

문제는 당신은 당신의 클래스 멤버와 동일한 이름의 임시 변수를 만들었습니다됩니다. setuiManager 안에는 ui이 데이터 멤버가 아닌 임시 변수를 참조한다고 가정합니다.

관련 문제