- Home /
How do I access variables initialized in a start function from another script?
So in one of my codes I'm trying to use the line
for(int i = 0; i < BPurchaseScript.character_list.Length; i++){
but I'm getting a null reference script because if I never run the BPurchaseScript the start function never happens which is what gives the value to the character_list.
public class BPurchaseScript : MonoBehaviour {
public static Item[] character_list;
BatMan BM = new BatMan();
SuperMan SM = new SuperMan();
void Start(){
//characters
BM = new BatMan();
SM = new SuperMan();
character_list = new Item[]{BM, SM};
}
}
So how can I use the BPurchaseScript.character_list when I don't run the start script?
What do you mean you don't run the Start script? It's run automatically since BPurchaseScript inherits from $$anonymous$$ono (assu$$anonymous$$g it's attached to GObj). In your top script, did you define a GetComponent/reference to BPurchaseScript, if so show syntax
Have you attached the BPurchaseScript behaviour to a GameObject? The code in Start() will not execute if the script is not attached to any object in the current scene.
If the scene with the start function isn't loaded then it stays null. That's the problem I'm trying to use the character_list without ever loading the scene where it gets initialized. And no I didn't use a GetComponent I made it public and static so I wouldn't have to.
Didn't notice the static tag; have something else in your scene call BPurchaseScript.Start() [before your top script does] since you really aren't using Start() in the normal sense. I'm not sure what you are doing with BPurchaseScript but it might be one that doesn't need to inherit from $$anonymous$$ono and thus would be available to any script in any scene
Answer by supernat · Mar 22, 2014 at 03:33 AM
3 Options I think.
The first is to initialize the character_list in the Awake function instead of Start. All Awake (I believe?) are called before any Start.
Second option: Before you use BPurchaseScript.character_list, check it for null:
if (BPurchaseScript.character_list != null) {
// do your for loop and anything else that needs the script
}
If you logically need to do the for loop right then and can't do it later (once the list is no longer null), the third option is to change the code above to:
static BatMan BM = new BatMan();
static SuperMan SM = new SuperMan();
public static Item[] character_list = new Item[]{BM, SM};
Then remove everything from the Start method. EDIT: Obviously, in the 3rd option, you've made BM and SM static, so if that's not an option, go with 1 or 2. Also note that there is actually a much deeper issue here. If you attach the script you currently have to multiple objects, the BM and SM will be created twice every time an object is instantiated, and the character_list will be recreated every time an object is instantiated. This means the character_list will always have only the last instantiated object's versions of SM and BM which might not be what you intended.
The awake didn't work :/ And also method two wouldn't work because it wouldn't run the code since it's always going to be null until the start method is ran from the other script which is in a completely different scene. And I dont really understand what you mean by the last instantiated objects of sm and bm, im never instantiating them they're just classes to hold values like power, speed, ect.
I verified that awake is always called before start. The problem then is that you don't have an instantiation of the script in your scene, as you pointed out. Awake/Start will only be called when a script is attached to something in the scene.
B$$anonymous$$ and S$$anonymous$$ are instantiated (i.e. B$$anonymous$$ = new Bat$$anonymous$$an) in two places in your script. If you want the B$$anonymous$$ and S$$anonymous$$ and character_list to all be valid regardless of which scene you're in, you should use option 3. $$anonymous$$ake everything static. That means this script doesn't have to be attached to anything anywhere (you still can) for the variables to be useful.
Your answer
Follow this Question
Related Questions
"Main" function? 3 Answers
Call method on "Start" of a built-in component? 1 Answer
Initialising List array for use in a custom Editor 1 Answer
"Failed to initialize graphics 1 Answer
Start() of Instantiated Prefabs Not Being Carried Out? 1 Answer