Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by j_perker · Mar 22, 2014 at 03:04 AM · errorstartinitialize

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?

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Mar 22, 2014 at 03:29 AM 0
Share

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

avatar image taylank · Mar 22, 2014 at 03:38 AM 0
Share

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.

avatar image j_perker · Mar 22, 2014 at 04:03 AM 0
Share

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.

avatar image getyour411 · Mar 22, 2014 at 04:21 AM 0
Share

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

avatar image j_perker · Mar 22, 2014 at 04:55 AM 0
Share

BPurchaseScript.Start() worked thanks.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image j_perker · Mar 22, 2014 at 03:57 AM 0
Share

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.

avatar image supernat · Mar 22, 2014 at 05:49 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges