Hi guys,
I have this code and don`t have problem:
using UnityEngine;
using System.Collections;
public class NuevoControl : MonoBehaviour {
public class Objetos
{
public int estadoCor;
public GameObject objetoCor = null;
}
public GameObject cora01;
public Objetos obj;
// Use this for initialization
void Start () {
obj = new Objetos();
obj.objetoCor = cora01;
}
// Update is called once per frame
void Update () {
}
}
But, i change object by a Vector, and the code fail and show error:
NullReferenceException: Object reference not set to an instance of an object
NuevoControl.Start () (at Assets/Scripts/NuevoControl.cs:21)
using UnityEngine;
using System.Collections;
public class NuevoControl : MonoBehaviour {
public class Objetos
{
public int estadoCor;
public GameObject objetoCor = null;
}
public GameObject cora01;
public Objetos[] obj;
// Use this for initialization
void Start () {
obj = new Objetos[1];
obj[0].objetoCor = cora01;
}
// Update is called once per frame
void Update () {
}
}
I’m doing wrong? Can anyone help me please?
Thanks.
Objetos[ ] is an array of references, all of which will be initially set to null. In the line:
obj[0].objetoCor = cora01;
You’re dereferencing a null reference. If you want the value to be something other than null, you’ll need to assign a non-null value to it yourself (e.g. by creating a new object using ‘new’).
A couple of other comments. First, I don’t know if this is relevant at all, but you can make your Objetos class directly serializable by using the ‘Serializable’ attribute. (I’m just mentioning this in case that’s really what you’re wanting and the public ‘cora01’ variable is just a workaround.)
Also, if you’re assigning a new value to ‘obj’ in your Start() function, there’s no particular reason to make it public. However, note that if Objetos is made to be serializable, you can make ‘obj’ public and it’ll be editable in the inspector just like any other serializable field.
Thanks for help me!
Ok, if I understand (sorry my programming level is very low), I must create a new Object, for each Object I need.
Is this the correct way??
using UnityEngine;
using System.Collections;
[System.Serializable]
class Objetos
{
public int estadoCor = 0;
public GameObject objetoCor = null;
}
public class NuevoControl : MonoBehaviour {
public GameObject cora01;
public GameObject cora02;
public GameObject cora03;
public GameObject cora04;
// Use this for initialization
void Start () {
Objetos[] obj;
obj = new Objetos[4];
obj[0] = new Objetos();
obj[1] = new Objetos();
obj[2] = new Objetos();
obj[3] = new Objetos();
obj[0].objetoCor = cora01;
obj[1].objetoCor = cora02;
obj[2].objetoCor = cora03;
obj[3].objetoCor = cora04;
}
Sorry my bad English, and thanks again.
Yes, that looks closer. Does that compile though? (In particular, I’m wondering about the visibility of the Objetos class.)
Note that there’s no particular reason to make Objetos serializable (not that I can think of at least) if you’re only going to be creating it dynamically in the Start() function. It looks to me though like what you really want is to make ‘obj’ a public member field; then you can assign game objects to its ‘objetoCor’ field directly. (If you don’t want ‘estadoCor’ to be editable via the inspector, you can make it private and expose it via a property instead.)