- Home /
Variable value persists when public but is lost between frames when private
I am at a complete loss. I have no idea how this is happening. Here's my PathFollower object:
class PathFollower extends AI { // And AI extends Ship
var curve : float = 1.0;
private var targetNode : Node;
function StartFollowing( node : Node ) {
targetNode = node.RandomNext();
if ( !targetNode ) {
gameObject.SetActiveRecursively(false);
}
}
function Update() {
super.Update();
if ( !targetNode ) { return; }
if ( (targetNode.transform.position - transform.position).sqrMagnitude < 0.3 ) {
StartFollowing(targetNode);
}
HomeTowards( targetNode.transform.position );
}
}
To create one of these, I do only this:
private function Trigger() {
count--;
var ship : Ship = spawner.Make( transform.position ).GetComponent(Ship);
var follower : PathFollower = ship.GetComponent(PathFollower);
if ( targetNode && follower ) {
follower.transform.position = targetNode.gameObject.transform.position;
follower.StartFollowing(targetNode);
}
if ( count <= 0 ) CancelInvoke(); //gameObject.active = false;
}
Now we get to the extremely fun part.
I originally build a simple Spawner that has just the code above and some timing stuff, and it works perfectly.
So I built my LevelHandler which controls spawns and waves and stuff, and just copied the code into that. Here's the complete, new function:
public function Process() {
if ( remain <= 0 ) return;
next-= Time.deltaTime;
if ( next <= 0 ) {
next+= rate;
remain--;
var s : Ship = spawner.Make( node.transform.position ).GetComponent(Ship);
var follower : PathFollower = ship.GetComponent(PathFollower);
if ( node && follower ) {
follower.StartFollowing(node);
}
s.AddListener(this);
}
}
This spawns the Ship. This gives the Ship a node to follow - using Debug statements, I can see that startFollowing is not only receiving a node, but it's also setting targetNode to something meaningful. And yet, in Update (even before super.Update), targetNode is null, meaning the Ship just sits still.
Process is called during an Update function. Trigger is called as a result of Invoke.
The original Trigger function still works correctly. Changing targetNode to public makes Process work correctly.
Protected behaves just like Private.
What is going on?
Answer by whydoidoit · Jun 24, 2012 at 10:40 PM
You appear to be getting a path follower from ship - but you have spawned a thing stored in s. I'm guessing that ship is the prefab instance or something - the reason it works when it is public is that the instantiate will copy a public variable, but not a private one unless it is marked with @SerializeField.
Will s.GetComponent(PathFollower) do it for you?
Haha.
Yes.
I cannot type.
For all you following along at home there's a stray "ship." up in there, which should have been "s."
Your answer
![](https://koobas.hobune.stream/wayback/20220613073229im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
When to use public or private variables for the inspector? 2 Answers
Has anyone had success with Facebook's graph API in Unity's Flash export? 1 Answer
Changing variable values not having any effect 2 Answers
Explanation on how to use functions, variables from other files 1 Answer
How faster is private variables over public variables? 1 Answer