Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by YumeYume · Sep 11, 2015 at 05:13 PM · nullstartinitializationsettersgetters

Getting a variable works in a Start function, but not in another one.

Sorry if the title isn't that clear.

I'm making a multiplayer shooter game, and now that the game is mostly playable (though not fun in the current state), I'm updating my code to introduce modularity. As I want the players to be able to choose a character with different stats, I made a class that adapts those stats in function of the user's choice, then I get these values in the scripts that needs them, so I can add more characters over time without having to modify the controls/health/etc scripts each time.

The class that contains the characters uses variables like this :

 public int Health { get; set; }

And a function set these variables according to user's choice.

Then, in the game scene, I get these variables using a GetComponent from the script where I need them. For my Player Controller script, it works perfectly, I tried several choices and it worked every time.

I proceed like this :

 //variables
 int movementSpeed;
 
 void Start() {
     movementSpeed = GetComponent<VariablesScript>().movementSpeed;
 }
 //then I use movementSpeed in the script, and it works

I did the exact same thing in my Player Health script, getting the max health the same way I do with movement speed, but for unknown reasons, the get isn't working, my variable is equal to null. I tried to get the variable in the update function, and it's working, but of course, I don't want the get to occur every single frame, it's just for initialization..

I don't understand why the exact same thing works in a script and not in another.. Any clues about why a get would work in a place and not in another ? Thanks in advance !

Comment
Add comment · Show 6
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 Dave-Carlile · Sep 11, 2015 at 05:36 PM 0
Share

You should also show us the script that isn't working. :)

avatar image Baste · Sep 11, 2015 at 05:39 PM 0
Share

Did you re-declare the movementSpeed variable in the script that's not working? This is a common error I see people do:

 //field
 int movementSpeed;

 void Start() {
     //local variable, no relation to the field
     int movementSpeed = GetComponent<VariablesScript>().movementSpeed;
 }

avatar image YumeYume · Sep 11, 2015 at 05:49 PM 0
Share

I get the movement speed in the control script, and health in the status script.

The code in the status script is :

 int health;
 
 void Start() {
     health = GetComponent<VariablesScript>().health;
 }

Exact same code than in the control script, but for another variable, in another script. (Note than the variables script, the control script and the health script are all attached to the player prefab, that's why I can't understand why it's working for only one of the two attempts..)

Just double checked, I didn't re-declare the variables, good thing you ask, that's an error I'm still doing sometimes..

avatar image Dave-Carlile · Sep 11, 2015 at 06:09 PM 1
Share

Are you adding VariableScript to the gameobject in the editor? Or do you add that at runtime? If at runtime then script execution order would be important - it would be possible for your health script Start function to run before VariableScript was added.

If that seems like it might be the case, you can control script execution order. $$anonymous$$ake sure whatever script adds VariableScript to the gameobject runs before anything that uses it.

avatar image Baste Dave-Carlile · Sep 11, 2015 at 06:19 PM 0
Share

Or just use Awake on the VariableScript, as that runs before Start.

avatar image Dave-Carlile Baste · Sep 11, 2015 at 06:26 PM 0
Share

Yes, or Awake on whatever script is adding VariableScript (if that is indeed what's happening).

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by YumeYume · Sep 11, 2015 at 06:31 PM

Thanks for your replies guys, but as I thought, that was a stupid error that I did completely out of the script..

My game is a multiplayer game, and I'm using UNet to handle the network. Of course, instantiating multiple instances of the same prefab with the same scripts would lead to problems like controlling all players because the script is enabled for all objects on all clients. To avoid that, I just have a script to enable the scripts for the local player only. Control and health scripts are in this list. But I forgot something for the health script.. They need to be disabled at start, and then enable them with the script. And I forgot to uncheck the enabled case in the editor for the health script.. A single click that changes everything.. Now all is working. Sigh.. Tired of doing newbie mistakes. Thanks for the execution order link though, I still have a lot of things to do, this link could be useful in the future. Thanks again !

Comment
Add comment · 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

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

29 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getter working, Setter not 2 Answers

Class array initialization on Start() gives -> Object reference not set to an instance of an object 1 Answer

How to create static component? 0 Answers

Constructor always creates null object --why? 0 Answers

Wave Spawn Script not working? From Null to Destroy Object. 0 Answers


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