Array (List) with multiple variable types?
Good day community ) I'm trying to create some sort of database. So I made 2 scripts. 1st is:
using System.Collections.Generic;
using UnityEngine;
public class Lists
{
public List<GameObject> gos;
public int qwty;
public int prefQwty;
public string[] prefPath;
public GameObject goFather;
public Vector3 pos;
public Lists(){}
}
And the 2nd one is:
using System.Collections.Generic;
using UnityEngine;
public class Adder
{
public int goQwty = 2;
public void Add()
{
Lists[] toCreate = new Lists[goQwty];
Debug.Log(toCreate);
toCreate[0].gos = new List<GameObject>();
toCreate[0].qwty = 10;
toCreate[0].prefQwty = toCreate[0].gos.Count;
toCreate[0].prefPath[0] = "/Stuff/Bullets/Bullet";
toCreate[0].goFather = GameObject.FindGameObjectWithTag("Player").transform.GetChild(0).gameObject;
toCreate[0].pos = new Vector3(.5f, 0, 0);
toCreate[1].gos = new List<GameObject>();
toCreate[1].qwty = 10;
toCreate[1].prefQwty = toCreate[0].gos.Count;
toCreate[1].prefPath[0] = "/Chars/Enemies/Red";
toCreate[1].goFather = GameMaster.Instance.enemies;
toCreate[1].pos = Vector3.zero;
}
}
But when I'm trying to "create" something using this database, it's always gives me this annoying "Object reference not set to an instance of an object" ( Can U tell me, what am I doing wrong pls)))
Answer by pako · Jan 13, 2018 at 11:23 AM
You haven't initialized properly the prefPath arrays.
Try:
toCreate[0].prefPath = new string[1];
toCreate[0].prefPath[0] = "/Stuff/Bullets/Bullet";
toCreate[1].prefPath = new string[1];
toCreate[1].prefPath[0] = "/Chars/Enemies/Red";
No, this didn't help( I think something wrong with Lists constructor.
You are right! it is the Lists constructor, you are not calling it anywhere!
This:
Lists[] toCreate = new Lists[goQwty];
... initializes an array object, which can hold Lists objects, but you have to create the Lists objects and store them in each array element, before accessing these elements. i.e. in effect the elements of toCreate array are null.
So:
using System.Collections.Generic;
using UnityEngine;
public class Adder
{
public int goQwty = 2;
public void Add()
{
Lists[] toCreate = new Lists[goQwty];
Debug.Log(toCreate);
for (int i = 0; i < goQwty; i++)
{
toCreate[i] = new Lists();
}
toCreate[0].gos = new List<GameObject>();
toCreate[0].qwty = 10;
toCreate[0].prefQwty = toCreate[0].gos.Count;
toCreate[0].prefPath = new string[1];
toCreate[0].prefPath[0] = "/Stuff/Bullets/Bullet";
toCreate[0].goFather = GameObject.FindGameObjectWithTag("Player").transform.GetChild(0).gameObject;
toCreate[0].pos = new Vector3(.5f, 0, 0);
toCreate[1].gos = new List<GameObject>();
toCreate[1].qwty = 10;
toCreate[1].prefQwty = toCreate[0].gos.Count;
toCreate[1].prefPath = new string[1];
toCreate[1].prefPath[0] = "/Chars/Enemies/Red";
toCreate[1].goFather = Game$$anonymous$$aster.Instance.enemies;
toCreate[1].pos = Vector3.zero;
}
}
$$anonymous$$an, thank u! U made my day!!! Always have this issue with Arrays)))
Answer by ransomink · Jan 13, 2018 at 02:33 PM
What line is giving you this error? It seems a GameObject or variable was not set: either in the inspector or the code, provided you can give us what line the error is on...
It's giving error on any line from:
toCreate[0].gos = new List<GameObject>();
Even if I comment it out the next line will give the same error.
I think something's wrong with Lists constructor.
I even tried to make this with List<> ins$$anonymous$$d of Array[]
First script:
public class Lists
{
public List<GameObject> gos;
public int qwty;
public string[] prefPath;
public int prefQwty;
public GameObject goFather;
public Vector3 pos;
public Lists(List<GameObject> Gos, int Qwty, string[] PrefPath, int PrefQwty, GameObject GoFather, Vector3 Pos)
{
Gos = gos;
Qwty = qwty;
PrefPath = prefPath;
PrefQwty = prefQwty;
GoFather = goFather;
Pos = pos;
}
}
And the second:
public class Adder
{
public int goQwty = 2;
public void Add()
{
List<Lists> toCreate = new List<Lists>();
//Bullets
toCreate.Add(new Lists(new List<GameObject>(), 10, new string[1] { "/Stuff/Bullets/Bullet" }, 1, GameObject.FindGameObjectWithTag("Player").transform.GetChild(0).gameObject, new Vector3(.5f, 0, 0)));
//Enemys
toCreate.Add(new Lists(new List<GameObject>(), 10, new string[1] { "/Chars/Enemies/Red" }, 1, Game$$anonymous$$aster.Instance.enemies, new Vector3(0, 0, 0)));
for (int i = 0; i < toCreate.Count; i++)
{
Pooler.Instance.Create(toCreate[i].gos, toCreate[i].qwty, toCreate[i].prefPath, toCreate[i].prefQwty, toCreate[i].goFather, toCreate[i].pos);
Debug.Log(toCreate[i].gos + " " + toCreate[i].qwty + " " + toCreate[i].prefPath + " " + toCreate[i].prefQwty + " " + toCreate[i].goFather + " " + toCreate[i].pos);
}
}
}
But Debug.Log shows me (0, 0, 0, 0, 0, 0); And I don't understand whats going on))