- Home /
A variable im trying to pull from another script is reseting when it shouldn't?
I have two scripts. The buttons script is trying to pull numberOfProjects variable, from the projectList script, every time the game object its attached to is cloned. Thus making a list of objects i can later turn into buttons. The problem i have having is that numberOfProjects keeps displaying 0 no matter what. And what makes it weird is that when i have it print the number its prints it at one first and then zero immediately afterwards. So here are my two scripts
projectList
// Project List
#pragma strict
// Inspector Variables
var numberOfProjects : int = 1; // Number of projects you have
var projectSelecterXPos : float; // X postions for all project selecter buttons
var projectSelecterYStartPos : float; // the starting y position that the NewProject function is based on
var projectSelecterGap : float; // the gap between your project selecter buttons
var projectNameList : String = "Project Name List "; // name of the box that hold your projects
var projectSelecterButton : Transform; // needed to make copys of text box
// Private Variables
private var projectManagerArray = Array(numberOfProjects); // may be needed later. not used yet
private var numberOfProjectsHolder : int = 0; // needed to hold a number for NewProject function
function Start ()
{
transform.position = Vector3(projectSelecterXPos,projectSelecterYStartPos,10);
}
function Update ()
{
NewProject();
}
function NewProject()
{
if(numberOfProjects>numberOfProjectsHolder)
{
var projectSelecterYPos = Array(numberOfProjects);
var x = projectSelecterYPos.length;
Instantiate(projectSelecterButton,Vector3(projectSelecterXPos,projectSelecterYStartPos - x * projectSelecterGap, 10), Quaternion.identity);
numberOfProjectsHolder++;
}
}
Buttons
// Project Buttons
#pragma strict
var projectNumber : int;
private var projectListScript : ProjectList;
function Start ()
{
projectListScript = GetComponent(ProjectList);
projectNumber = projectListScript.numberOfProjects;
}
function Update ()
{
print(projectNumber);
}
This is what the console displays in this order.
1 UnityEngine.MonoBehaviour:print(Object) Project buttons:Update() (at Assets/Project buttons.js:17)
NullReferenceException: Object reference not set to an instance of an object Project buttons.Start () (at Assets/Project buttons.js:11)
0 UnityEngine.MonoBehaviour:print(Object) Project buttons:Update() (at Assets/Project buttons.js:17)
Is the ProjectList script properly connected to the Buttons script? It's a compiler error, so it's still an old version of the code that you run.
What do you mean properly connected. and after a little bit of experimenting i noticed that if i change number of projects it still prints the number one along with the number i changed it to.
That's probably because in the Start() method you reinitalise the variable. (line 19) Try deleting that, and tell us what output you get.
he means that the code is saying
getcomponent(projectlist)
error the component project list is not a component of the game object the button script is attached to
so for this code to work
there should be a game object and the script projectlist should be attached to it. as well the script buttons.
see that null reference exception.
what it's saying is that when it tries to resolve
projectscript.numberofprojects it comes back null
since numberofprojects is a variable with a default value in the projectscipt you have shwon
the only possibility is that getcomponent is returning null
if you were to do
if( ! getcomponent(projectscript){ debug.log("there is no attached project script"); }
it would print that i'm betting. Because the component isn't attached.
So you need to make sure both scripts are attached to the same object.
the compiler doesnt check as your typing because you may be adding or subtracting components in the code. so this error wont pop up until run-time.