Hi, I try to rewrite js controller script in C Sharp. In my js file I’ve many classes whose only storing some variables. Similar construction won’t work in C#, because those variables are unreachable. For example
js
class Aaa{
var abc = 1.5;
}
var d:Aaa;
in c# i try this
class Aaa{
public float abc = 1.5f;
}
public Aaa d = new Aaa();
d.abc = <new value>
This construct give me NullReferenceException. Where I’ve made mistake?
Try this:
public class Aaa { //add "public" modificator, without variable/class is "private"
public float abc = 1.5f;
}
public Aaa d = new Aaa();
void Start() {
d.abc = 2.0f;
}
Initialization your variable of class in Awake() or Start(). I hope that it will help you.
Your problem is that your code is recursively constructing…
Your class Aaa has a field d which is initialized by constructing a new Aaa,
the new Aaa has a field d which is initialized by constructing another Aaa,
that has a field d which is initialized by constructing another Aaa…
And it goes on forever, but since computer are finite, it eventually runs out of stack room and crashes.