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

problems with putting objects in al list

Discussion in 'Scripting' started by R0b_, Nov 19, 2018.

  1. R0b_

    R0b_

    Joined:
    Jul 11, 2018
    Posts:
    25
    when I try to put this.object in the MyTeam List from EnemieAI it doesn't do that, while when I put it in the Other team list it works just fine.

    this is these are the errors i got:

    NullReferenceException: Object reference not set to an instance of an object
    Enemie.Start () (at Assets/scripts/Objects/Enemie.cs:37)

    NullReferenceException: Object reference not set to an instance of an object
    Enemie.Start () (at Assets/scripts/Objects/Enemie.cs:30)

    NullReferenceException: Object reference not set to an instance of an object
    EnemieAI.CalculateInputs () (at Assets/scripts/AI/EnemieAI.cs:28)
    EnemieAI.MoveMembers () (at Assets/scripts/AI/EnemieAI.cs:72)
    AIManager.Update () (at Assets/scripts/AI/AIManager.cs:36)

    here are the scripts:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class Enemie : MonoBehaviour
    6. {
    7.     public GameObject Me;
    8.     private AIManager Manager;
    9.     public Material Dead;
    10.     public bool Alive = true;
    11.     public string Team;
    12.     private void Awake()
    13.     {
    14.         Manager = GameObject.Find("Manager").GetComponent<AIManager>();
    15.         Debug.Log(Me + ": Set manager object");
    16.         Me = this.gameObject;
    17.         Debug.Log("Set Me: " + Me + this.gameObject);
    18.     }
    19.     private void Start()
    20.     {
    21.         Manager.GetComponent<AIManager>().AllMembers.Add(Me);
    22.         Debug.Log(Me + ": Add myself to Allmembers in the AImanager");
    23.         if (Team == "Red")
    24.         {
    25.             Manager.Red.AliveMembers += 1;
    26.             Manager.Red.MyTeam.Add(this.gameObject);
    27.             Manager.Blue.OtherTeam.Add(this.gameObject);
    28.             Debug.Log("Add myself to the red team");
    29.         } else if (Team == "Blue")
    30.         {
    31.             Manager.Blue.AliveMembers += 1;
    32.             Manager.Blue.MyTeam.Add(this.gameObject);
    33.             Manager.Red.OtherTeam.Add(this.gameObject);
    34.             Debug.Log("Add myself to the Blue team");
    35.         } else
    36.         {
    37.             Debug.Log("Team: " + Team + " does not exist");
    38.         }
    39.     }
    40.     public void OnCollisionEnter(Collision bullet)
    41.     {
    42.         if (bullet.gameObject.name == "bullet")
    43.         {
    44.             Die();
    45.             Debug.Log(Me + ": " + "Dies because of bullet");
    46.         }
    47.     }
    48.     void Die()
    49.     {
    50.         Alive = false;
    51.         Me.GetComponentInChildren<SphereCollider>().enabled = false;
    52.         Debug.Log(Me + ": " + "Die(): disable sphere collider");
    53.         //Debug.Log(Me + ": " + "");
    54.         if (Team == "Red")
    55.         {
    56.             Manager.Red.AliveMembers -= 1;
    57.             Debug.Log(Me + ": " + "Remove myself from alivemembers list");
    58.         }
    59.         else if (Team == "Blue")
    60.         {
    61.             Manager.Blue.AliveMembers -= 1;
    62.             Debug.Log(Me + ": " + "Remove myself from alivemembers list");
    63.         }
    64.         else
    65.         {
    66.             Debug.Log("Team: " + Team + " does not exist");
    67.         }
    68.         Me.GetComponentInChildren<MeshRenderer>().material = Dead;
    69.         Debug.Log(Me + ": " + "Die(): change material to: Dead");
    70.     }
    71. }
    72.  
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class EnemieAI
    6. {
    7.     public static int[] NeuralNetworkLayers = {1, 2, 2, 1};
    8.     public NeuralNetwork EnemieNetwork = new NeuralNetwork(NeuralNetworkLayers);
    9.     private float[] NeuralNetworkOutputs;
    10.     public List<GameObject> MyTeam;
    11.     public List<GameObject> OtherTeam;
    12.     public List<GameObject> Obstacles;
    13.     public int AliveMembers;
    14.     public float MemberSpeed = 5f;
    15.     public GameObject bullet;
    16.     public EnemieAI(ref List<GameObject> _Obstacles)
    17.     {
    18.         Obstacles = _Obstacles;
    19.     }
    20.     public float[] CalculateInputs()
    21.     {
    22.         List<float> Inputs = new List<float>();
    23.        
    24.         foreach (GameObject Member in MyTeam)
    25.         {
    26.             Inputs.Add(Member.transform.position.x);
    27.             Inputs.Add(Member.transform.position.y);
    28.             Inputs.Add(Member.transform.rotation.z);
    29.             if (Member.GetComponent<Enemie>().Alive)
    30.             {
    31.                 Inputs.Add(1);
    32.             } else
    33.             {
    34.                 Inputs.Add(-1);
    35.             }
    36.         }
    37.         foreach (GameObject Member in OtherTeam)
    38.         {
    39.             Inputs.Add(Member.transform.position.x);
    40.             Inputs.Add(Member.transform.position.y);
    41.             Inputs.Add(Member.transform.rotation.z);
    42.             if (Member.GetComponent<Enemie>().Alive)
    43.             {
    44.                 Inputs.Add(1);
    45.             }
    46.             else
    47.             {
    48.                 Inputs.Add(-1);
    49.             }
    50.         }
    51.         foreach (GameObject Obstacle in Obstacles)
    52.         {
    53.             Inputs.Add(Obstacle.transform.position.x);
    54.             Inputs.Add(Obstacle.transform.position.y);
    55.             Inputs.Add(Obstacle.transform.lossyScale.x);
    56.             Inputs.Add(Obstacle.transform.lossyScale.y);
    57.         }
    58.         return Inputs.ToArray();
    59.     }
    60.     public void MoveMembers()
    61.     {
    62.         NeuralNetworkOutputs = EnemieNetwork.FeedForward(CalculateInputs());
    63.         Debug.Log("Inputs: " + EnemieNetwork.FeedForward(CalculateInputs()) + "| Outputs: " + NeuralNetworkOutputs);
    64.         for (int x = 0; x < MyTeam.Capacity; x++)
    65.         {
    66.             MyTeam[x].transform.rotation = Quaternion.Euler(MyTeam[x].transform.rotation.x, MyTeam[x].transform.rotation.y, NeuralNetworkOutputs[x * 4] * 360);
    67.             float[] MoveDirection = new float[1];
    68.             if (NeuralNetworkOutputs[x * 4 + 1] > 0)
    69.             {
    70.                 MoveDirection[0] = MemberSpeed;
    71.             } else
    72.             {
    73.                 MoveDirection[0] = -MemberSpeed;
    74.             }
    75.             if (NeuralNetworkOutputs[x * 4 + 2] > 0)
    76.             {
    77.                 MoveDirection[1] = MemberSpeed;
    78.             }
    79.             else
    80.             {
    81.                 MoveDirection[1] = -MemberSpeed;
    82.             }
    83.             MyTeam[x].GetComponent<Rigidbody>().velocity = new Vector3(MoveDirection[0], MoveDirection[1], 0);
    84.             if (NeuralNetworkOutputs[x * 4 + 3] > 0)
    85.             {
    86.                 MonoBehaviour.Instantiate(bullet, MyTeam[x].transform.position, Quaternion.Euler(MyTeam[x].transform.rotation.x, MyTeam[x].transform.rotation.y, MyTeam[x].transform.rotation.z));
    87.             }
    88.         }
    89.     }
    90. }
    91.  
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class AIManager : MonoBehaviour
    6. {
    7.     public EnemieAI Blue;
    8.     public EnemieAI Red;
    9.     public EnemieAI Winner;
    10.     public List<GameObject> AllMembers;
    11.     public List<GameObject> AllObstacles;
    12.     public void Awake()
    13.     {
    14.         Blue = new EnemieAI(ref AllObstacles);
    15.         Red = new EnemieAI(ref AllObstacles);
    16.     }
    17.     public void Update()
    18.     {
    19.         if (Red.AliveMembers == 0)
    20.         {
    21.             Winner = Blue;
    22.         }
    23.         if (Blue.AliveMembers == 0)
    24.         {
    25.             Winner = Red;
    26.         }
    27.         Debug.Log("Moving members");
    28.         Red.MoveMembers();
    29.         Blue.MoveMembers();
    30.     }
    31.     void StartMatch()
    32.     {
    33.         foreach (GameObject Member in AllMembers)
    34.         {
    35.             Destroy(Member);
    36.         }
    37.         Red = new EnemieAI(ref AllObstacles);
    38.         Red.EnemieNetwork = Winner.EnemieNetwork;
    39.         Blue = new EnemieAI(ref AllObstacles);
    40.         Blue.EnemieNetwork = Winner.EnemieNetwork;
    41.         Blue.EnemieNetwork.Mutate();
    42.         Winner = null;
    43.         int _Random = Random.Range(-1, 1);
    44.         if (_Random > 0)
    45.         {
    46.         } else
    47.         {
    48.         }
    49.     }
    50. }
    51.  
     
  2. dpgdemos

    dpgdemos

    Joined:
    Apr 28, 2014
    Posts:
    24
    Is the object containing the AIManager component missing a reference to the
    Code (csharp):
    1. public EnemieAI Blue;
    object in the editor?
     
  3. R0b_

    R0b_

    Joined:
    Jul 11, 2018
    Posts:
    25
    What do you mean?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It looks like your line numbers have shifted when you posted your code. I'd ask for the specific lines with the errors, but you shouldn't need people's help with this. The way you tackle null reference errors is the exact same process every time. First you go to the first null reference error, then go to the line it occurs and count the reference variables on that line. If there is only one reference type used on that line, then you know what variable is null. Then just make it not null before trying to use it. If there is more than one reference type variable on that line, then add debugging beforehand to figure out which one it is, something like below. Once you figure out which one is null, again make it not null.

    Code (csharp):
    1. if (suspiciousVariable == null)
    2. {
    3.     Debug.Log("suspiciousVariable is the one that is null");
    4. }
     
  5. R0b_

    R0b_

    Joined:
    Jul 11, 2018
    Posts:
    25
    The thing is, both of these lines of code are basically identical, but the he one on the top doesn't work while the one on the bottom does:

    Enemie.cs
    Code (csharp):
    1.  
    2.             Manager.Red.MyTeam.Add(this.gameObject);
    3.             Manager.Blue.OtherTeam.Add(this.gameObject);
    4.  

    both of the lists I try to put "this.gameObject" in are also created in the same way:

    EnemieAI.cs
    Code (csharp):
    1.  
    2.             public List<GameObject> MyTeam;
    3.             public List<GameObject> OtherTeam;
    4.  
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What makes you think the second line works? When you hit a null ref exception adding to MyTeam in Start, the rest of Start won't be run, so you don't even get to that next line. I don't see anywhere you are initializing any of your lists in your EnemieAI class, so MyTeam, OtherTeam, etc, are all null when you are trying to add to them.
     
  7. dpgdemos

    dpgdemos

    Joined:
    Apr 28, 2014
    Posts:
    24
    What I'm meaning is that when you have a public object/field, you should be able to see said object/field within the script component attached to the object in the inspector. Are you missing a reference to the object giving you the errors? Is the EnemieAI reference empty in the inspector?