I’ve been working on a pathing system, where I can let my developer click on a button. This creates a new node, and sets the ‘nextNode’ variable in the previous node to this new node.
This all happens while in Editor mode for easy acces.
All seemed to be going well. That is, untill I hit ‘play’.
You see, Unity then decided that the variables I set, namely nextNode (amongst other things) didn’t really matter, and he’d best reset everything to null…
I obviously was not so happy with this.
So anyway, after poking around here and there, I noticed there was a notable difference between setting variables private and public.
So after setting them to public, and quickly testing this getup I thought I saw what was the end of my woes.
But not for long…
It seems that somewhere along the way things go wrong.
While I initialise my nextNode variable as ‘null’, and set it only upon creating a new node, it somehow seems to already contain a variable.
Note: the first few times it works as it should. Only upon creating the 3rd, 4th or so node does it go wrong. nextNode then seems to suddenly point to the 2-3’rd node, creating a loop.
Anyway, I’ll add some simple scripts to illustrate (named Bur and Hur respectively).
This is the main script:
@script ExecuteInEditMode()
public var Foo = 0;
Debug.Log("pre " +Foo);
//public nextFoo : GameObject = null;
function Start(){
Debug.LogWarning("start " +Foo);
}
function SetFoo(val){
Foo = val;
Debug.LogError("setting! " +Foo);
}
/*function NewFoo() {
var newFoo = Instantiate(Foo);
nextFoo = newFoo;
}*/
And this is my EditorGUI extention.
@CustomEditor (Bur)
class Hur extends Editor {
function OnInspectorGUI () {
if (GUILayout.Button("Foo Me")) {
target.SendMessage("SetFoo", 20);
}
}
}
The behaviour of my nodes is somewhat the same as ‘NewFoo’. A new node gets created, and the internal parameter is set to that new object.
nextFoo should imo always be null, untill I set it. When nextFoo is private, all works exactly as it should.
But when I set it to public, nextFoo sometimes already containes a value (other than null), without me touching it.
I get no errors whatsoever. Anyone with suggestions?