Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

my Public Script Array Won't Show In Inspector..?

Discussion in 'Scripting' started by xAmrxxxx, Mar 24, 2016.

  1. xAmrxxxx

    xAmrxxxx

    Joined:
    Jan 5, 2016
    Posts:
    56
    ok so i've been trying to write a GameManager script from Tanks! tutorial and for some reason my Script Array won't show in the inspector although all other public variables do

    (i got "object reference not set to an instance of an object" error in lines 27,60,72) in my game manager script too

    game manager Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. namespace Complete
    8. {
    9. public class gameManaging : MonoBehaviour {
    10.     public  TankManaging[] tanksSCRIPT;
    11.     private WaitForSeconds startTime;
    12.     private WaitForSeconds endTime;
    13.     public GameObject TankPrefab;
    14.     private TankManaging ScriptROUND;
    15.     public CamRig camScript;
    16.     public Canvas TextMessage;
    17.     int Nrounds;
    18.     // Use this for initialization
    19.     void Start () {
    20.         startTime = new WaitForSeconds (3f);
    21.         endTime = new WaitForSeconds (5f);
    22.         StartCoroutine (GameLoop ());
    23.  
    24.     }
    25.     void SpawnAllTanks()
    26.     {
    27.         for (int i = 0; i <tanksSCRIPT.Length; i++)
    28.         {
    29.             tanksSCRIPT [i].TankInstance = Instantiate (TankPrefab, tanksSCRIPT [i].SpawnPoint.position,
    30.                 tanksSCRIPT [i].SpawnPoint.rotation) as GameObject;
    31.             tanksSCRIPT [i].Setup ();
    32.             tanksSCRIPT [i].playerCanvas.gameObject.SetActive (false);
    33.  
    34.         }
    35.     }
    36.    
    37.     // Update is called once per frame
    38.     IEnumerator GameLoop()
    39.     {
    40.         yield return startTime ;
    41.         yield return StartCoroutine(RoundStart());
    42.         yield return StartCoroutine( RoundPlaying());
    43.         yield return StartCoroutine( RoundEnding());
    44.         /*if (!GameWinner) {
    45.             StartCoroutine (GameLoop());
    46.         } else {
    47.             Application.LoadLevel (0);
    48.         }*/
    49.     }
    50.     IEnumerator RoundStart()
    51.     {
    52.         SpawnAllTanks ();
    53.         SetCameraTargets ();
    54.         StartMessage ();
    55.         yield return startTime;
    56.  
    57.     }
    58.     IEnumerator RoundPlaying()
    59.     {
    60.         for(int i = 0 ; i<=tanksSCRIPT.Length ; i++)
    61.         {
    62.             tanksSCRIPT [i].EnableControl ();
    63.         }
    64.         while(!ONETANKLEFT() )
    65.         {
    66.             yield return null;
    67.         }
    68.  
    69.     }
    70.     IEnumerator RoundEnding()
    71.     {
    72.         for(int i = 0;i<tanksSCRIPT.Length;i++)
    73.         {
    74.             tanksSCRIPT [i].DisableControl ();
    75.         }
    76.  
    77.         yield return endTime;
    78.     }
    79.     void StartMessage()
    80.     {
    81.         TextMessage.GetComponentInChildren<Text> ().text = string.Empty;
    82.         Nrounds++;
    83.         TextMessage.GetComponentInChildren<Text> ().text = "ROUND " + Nrounds;
    84.     }
    85.     void SetCameraTargets()
    86.     {
    87.         Transform[]targets = new Transform[camScript.Enemies.Length];
    88.         for(int i = 0;i<=targets.Length;i++)
    89.         {
    90.             targets [i].position = tanksSCRIPT [i].TankInstance.transform.position;
    91.         }
    92.         camScript.Enemies = targets;
    93.     }
    94.     private bool ONETANKLEFT()
    95.     {
    96.         int tanksLEFT = 0;
    97.         for(int i =0;i<=tanksSCRIPT.Length;i++)
    98.         {
    99.             if(tanksSCRIPT[i].TankInstance.activeSelf)
    100.             {
    101.                 tanksLEFT++;
    102.             }
    103.  
    104.         }
    105.         return tanksLEFT <= 1;
    106.     }
    107. }
    108. }
    tank managing script (the one i want to make array for..)
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3.  
    4.  
    5. public class TankManaging
    6.     {
    7.     private int PlayerNumber;
    8.     private  Shooting shootigScript;
    9.     private  MovingTank movingScript;
    10.     [HideInInspector]public GameObject TankInstance;
    11.     [HideInInspector]public Transform SpawnPoint;
    12.     [HideInInspector]public Color playerColor;
    13.     public string TankName;
    14.     public  Canvas playerCanvas;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.    
    19.     }
    20.     public void Setup()
    21.     {
    22.         shootigScript = TankInstance.GetComponent<Shooting> ();
    23.         movingScript = TankInstance.GetComponent<MovingTank> ();
    24.         playerCanvas = TankInstance.GetComponent<Canvas> ();
    25.         MeshRenderer[] renderers = TankInstance.GetComponentsInChildren<MeshRenderer> ();
    26.  
    27.         for(int i =0;i<=renderers.Length;i++)
    28.         {
    29.             renderers [i].material.color = playerColor;
    30.         }
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Reset()
    35.     {
    36.         TankInstance.transform.position = SpawnPoint.transform.position;
    37.         TankInstance.transform.rotation = SpawnPoint.transform.rotation;
    38.         playerCanvas.gameObject.SetActive (false);
    39.         playerCanvas.gameObject.SetActive (true);
    40.     }
    41.     public  void EnableControl()
    42.     {
    43.         shootigScript.enabled = true;
    44.         movingScript.enabled = true;
    45.         playerCanvas.gameObject.SetActive (true);
    46.  
    47.     }
    48.     public  void DisableControl()
    49.     {
    50.         shootigScript.enabled = false;
    51.         movingScript.enabled = false;
    52.         playerCanvas.gameObject.SetActive (false);
    53.  
    54.     }
    55.  
    56. }
    57.  
     
  2. OrbitSoft

    OrbitSoft

    Joined:
    Sep 21, 2013
    Posts:
    54
    When using custom classes with arrays or lists the class has to be serializable, you have to add [System.Serializable] like this:
    Code (CSharp):
    1. //using here
    2.  
    3. [System.Serializable]
    4. public class TankManaging
     
    xAmrxxxx likes this.