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 /
This question was closed May 23, 2014 at 11:02 PM by PippyLongbeard for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by PippyLongbeard · May 23, 2014 at 10:27 PM · c#runtimevariablesassignincorrectly

Very, very strange float assignment problem.

Hello. I've currently got an utterly baffling problem with assigning variables in one of my scripts. Basically, the value I'm assigning is completely different from the one being used in the game. The script below holds data for weapons, and is very simple. Code:

 using UnityEngine;
 using System.Collections;
 
 public class WeaponStats : MonoBehaviour {
 
     WeaponEquip weaponEquip;
     //Scimitar
 
     [HideInInspector]public float scim_one_speed = 4.0f;
     [HideInInspector]public int scim_one_damage = 50;
 
     //Dagger
     [HideInInspector]public float dagger_one_speed = 0.0f;
     [HideInInspector]public int dagger_one_damage = 34;
     void Start () {
         Debug.Log (scim_one_speed);
         Debug.Log (dagger_one_speed);
     }
 }

You'd expect the console to print off '4.0' and '0.0' as those two variables. Nope. The strangest part is that the console doesn't just print off two incorrect values, it prints off four. According to Unity, 'scim_one_speed' is simultaneously '1' and '0', while 'dagger_one_speed' is simultaneously '0.75' and '0' at run-time. If anyone has any idea why this is happening, I'd be very grateful for their help.

Comment
Add comment · Show 2
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 Tanshaydar · May 23, 2014 at 10:42 PM 0
Share

Do you have any other script that access to these variables? Since they are public, they are pretty much accessible. Try making them private and see if they works. Secondly, assign values in Awake function ins$$anonymous$$d of at the instantiation.

avatar image PippyLongbeard · May 23, 2014 at 10:44 PM 0
Share

[SOLVED] $$anonymous$$an, I hate how I usually find the answer right after I post a UnityAnswers query. I guess the problem was that since the variables were public and serialized, they were saving some value (the "1","0.75","0","1") from somewhere. Adding the '[System.NonSerialized]" attribute fixed the issue. I'm not entirely sure if I should post this as an answer, so I'll just keep it as a comment for now. Thanks anyways, guys. [EDIT] This is a needless solution. The reason I needed to add this attribute is answered by Eric5h5 below.

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Eric5h5 · May 23, 2014 at 10:43 PM

You attached the script to two different objects, and the variables hold the values that Unity is printing. If it says that scim_one_speed is 1, then it is. Since they're public, the values do not come from your code, aside from the initial assignment, they come from the inspector. If you remove the HideInInspectors you can see what the values actually are.

Comment
Add comment · Show 1 · 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 PippyLongbeard · May 23, 2014 at 10:59 PM 0
Share

So I guess I sort of had half the right answer. Non-serialization fixed it because I had two copies on my GameObject that I didn't notice. So, yeah, you're right. Thanks.

avatar image
0

Answer by Jeff-Kesselman · May 23, 2014 at 10:42 PM

You probably have two copies of this component out there.

Either they are both on the same GameObject, or you have one a piece on multiple game objects. Its ALSO possible that you have the same basic code in two different script files.

Fix that first so there is only one and it really is this current component. Then attack the values if there is still an issue.

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

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Multiple Cars not working 1 Answer

C# Inheritance - Reference the INSTANCE of a variable, is it possible? 1 Answer

Distribute terrain in zones 3 Answers

Reassign Variable Value C# 2 Answers

Script variable can be referenced in Start but not in update. 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