Search Unity

MissingReferenceException

Discussion in 'Scripting' started by ratxrat, Nov 24, 2016.

  1. ratxrat

    ratxrat

    Joined:
    Nov 7, 2016
    Posts:
    19
    hay hay, i create code for my caracter costumize, when button j press change clothes, this my code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Costumize : MonoBehaviour {
    5.  
    6.     public GameObject[] baju; // aray clothes gameobject
    7.    
    8.     int bagian = 0;
    9.  
    10.    
    11.     void Start () {
    12.    
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void test()
    17.     {
    18.         baju[bagian].gameObject.SetActive (true);
    19.         bagian += 1;
    20.     }
    21.     void Update () {
    22.             if (Input.GetKeyDown (KeyCode.J)) {
    23.  
    24.  
    25.  
    26.                 if (bagian == 0 ){
    27.                 for (int x = 0 ; x < baju.Length ; x++ ){
    28.                     baju[x].gameObject.SetActive (false);
    29.                 }
    30.  
    31.                 Invoke ( "test", 1);
    32.  
    33.        
    34.            
    35.              }
    36.             else if ( bagian == 1)
    37.             {
    38.                 for (int x = 0 ; x < baju.Length ; x++ ){
    39.                     baju[x].gameObject.SetActive (false);
    40.                 }
    41.  
    42.                 baju[bagian].gameObject.SetActive ( true);
    43.                 bagian =0;
    44.  
    45.  
    46.             }
    47.  
    48.         }
    49.    
    50.     }
    51. }
    this code work fine but in my console show error

    i want clean code, any one show me where problem?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    What is costumize on? Either the value of Costumize is null or the object is being destroyed. It may be because of your Invoke if you are turning off all objects. But would have to know more about your setup.
     
  3. ratxrat

    ratxrat

    Joined:
    Nov 7, 2016
    Posts:
    19
    Code (CSharp):
    1. for (int x = 0 ; x < baju.Length ; x++ ){
    2.                     baju[x].gameObject.SetActive (false);
    3.                 }
    this loop for deactive object from my hierarchy, in hierarchy im un check for active for all my clothes object , i mean deactive object use loop then active again use invoke
    Code (CSharp):
    1. void test()
    2.     {
    3.         baju[bagian].gameObject.SetActive (true);
    4.         bagian += 1;
    5.     }
    when pust J object next from object[0] to next object




    "Sory for my bad english" :D:D
     
  4. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    What are you doing to the gameobject the Customize class is on? It seems like you destroy that somewhere so when Invoke wants to invoke the method after a second the customize class is destroyed.
     
  5. ratxrat

    ratxrat

    Joined:
    Nov 7, 2016
    Posts:
    19
    this problem solved by my self thank for interest :D

    this my fix code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Costumize : MonoBehaviour {
    5.  
    6.     public GameObject[] baju;
    7.  
    8.  
    9.  
    10.  
    11.     int bagian = 0; // baju
    12.    
    13.  
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.    
    18.     }
    19.    
    20.     // Update is called once per frame
    21.    
    22.     void Update () {
    23.             if (Input.GetKeyDown (KeyCode.J)) {
    24.                 if (bagian < baju.Length )
    25.                 {
    26.                     for (int x = 0 ; x < baju.Length ; x++ ){
    27.                         baju[x].gameObject.SetActive (false);
    28.                     }
    29.                     baju[bagian].gameObject.SetActive (true);
    30.                    
    31.                     bagian ++;
    32.                     Debug.Log (bagian);
    33.                 }
    34.                 else if ( bagian >= baju.Length)
    35.                 {
    36.                
    37.                     for (int x = 0 ; x < baju.Length ; x++ ){
    38.                         baju[x].gameObject.SetActive (false);
    39.                     }
    40.  
    41.                     baju[baju.Length-1].gameObject.SetActive ( true);
    42.                     bagian =0;
    43.                     Debug.Log (bagian);
    44.                 }
    45.  
    46.         }
    47. }