When a class is created inside a function it does not contain members!? C#

I made a class like this:

  • [System.Serializable] *
  • public class nodeAttributes*
  • {*
  • public Transform nodeTransform;*
  • public Transform aimTransform;*
  • } *

and then inside of void Start() I do this:

nodeAttributes attributes = new nodeAttributes[6];

but if I try to access a member of the class, like nodeTransform, by going like this:

print(attributes[0].nodeTransform);

It gives me a null reference exception.

When I watch the code with the debugger I notice that when I create the attributes array inside of Void Start(), the resulting array does not contain the nodeTransform or aimTransform’s inside of it.

So it’s seeming to me like, if I create an array of a Class inside of a function, then the resulting array will not contain the members of the class. Is this correct? Is there anyway to change that?

That’s expected. The entries in the attributes array are null until you initialize them. (However, structs are automatically initialized to the default values, unlike classes.)

–Eric

how do you intialize?

Seriously, as a C# developer you should know that already or do some C# tutorials first before attempting to program a game :slight_smile:

btw., in C# we use CamelCase notation for class names, Methods and Properties (NodeAttributes, NOT nodeAttributes). Even though not required, it’s an recommended notation for better readability of code and most if not all C# developers do it, so you can easier see that it’s an class or Property and not an variable.

[System.Serializable] 
public class nodeAttributes {
   public Transform nodeTransform;
   public Transform aimTransform;
}

nodeAttributes attributes = new nodeAttributes[6];

for(int i = 0; i<attributes = attributes.Lenght; i++) {
   nodeAttributes att = new nodeAttributes();

   // do extra initializations here (optional)
   att.nodeTransform = // assign a transform here
   att.aimTransform = // assign another transform here
   
   attribtues[i] = att;
}

Unless the “attributes” variable is a public variable of MonoBehaviour (i.e. you can see it in Inspector Window) it won’t be initiallized automatically.

However, unless you’re going to use private members or methods in your “nodeAttributes” you can also change the “public class nodeAttributes {” into “public struct nodeAttributes {” too, as Eric said

Not only can you not expect your array to be filled, in Unity you cannot create Transforms like this on their own anyways. They are components and thus need to be associated with GameObjects. You can do this:

for (int i=0; i<nodeAttributes.Length;i++) {
nodeAttributes*.nodeTransform = new GameObject().transform;*
nodeAttributes*.aimTransform = new GameObject().transform;*
}

Now wait a second In JS I can define a class, define variables in it, and give them values. Is this not the same in C#?

class cMesh{
	var name : String="";
	var vertices : Vector3[];
	var normals : Vector3[];
	var tangents : Vector4[];
	var uv : Vector2[];
	var triangles : int[];
	var material : String="";
}

or is this question more geared towards the type arrays? Where you would have to create a constructor for the class…

	function cMesh(){
		vertices=new Vector3[5];
		vertices[0]=Vector3(1,1,1);
		vertices[1]=Vector3(1,1,1);
		vertices[2]=Vector3(1,1,1);
		vertices[3]=Vector3(1,1,1);
		vertices[4]=Vector3(1,1,1);
	}

I’m unfortunately not a C# developer :frowning:
I was a UnityScript developer, and now I’ve jumped on C# just like 4 months ago and have been learning as I go. I’m not really the go through tutorials beforehand type…