Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

inspector assigning random numbers to variables

Discussion in 'Scripting' started by ChuckieGreen, Mar 2, 2018.

  1. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    352
    Is there a way to see where the inspector is getting its values from? Ive checked the script that the values are being assigned on, and there is nothing in the script that gives the variables value, and in the inspector they are all set to 0 until the game starts.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hm.. I think you just have to check your code?

    You get some values (not zero) when the game starts, but can't figure out why?
     
  3. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    352
    mm I will have another look through the code, but I really cant see the issue, The strange thing is, I'm sure I did set those values when starting the project, but the code has since changed. Its like its not reading the new code.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Did you set them when they were declared, like:
    Code (csharp):
    1. float speed = 5;
    and then the inspector now says 0? If that's the case, you have to modify the value in the inspector, and that'll be the default value, as the inspector values override declared values.

    Please explain more if that's not the case..
     
  5. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    352
    I am sure when I first started it, i declared

    Code (CSharp):
    1. public int PistolbulletsCollected = 5;
    But I have since changed the code to only
    Code (CSharp):
    1. public int PistolbulletsCollected;
    Before I click play the inspector says 0, but in game it changes to 5. I dont know if it is the best way of doing it but I might just set all the ones causing and issue to 0 in the awake function
     
  6. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    352

    Edit - tried setting it value to 0 in the awake function, didnt change anything
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That is strange.. if you do not change it in awake or start or somewhere else, I'm pretty sure that shouldn't be happening.

    Based on your edit, you must be changing the code somewhere else...Try looking to be sure, search for that variable name in all of your source, perhaps?
     
  8. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Is it possible that you have changed those values once and it got serialized?
    What does the inspector says? Does it says 5? Or if it isn't, please, try to change it in the inspector to something (e.g. 20), save your scene and then try to change it back to 0 and save the scene.

    I believe it's a serialization issue.
     
  9. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    352
    I will go through everything and double check it, I must be missing something somewhere. I never serialized that value, and when i changed it in the awake function it continued to say 5.
     
  10. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Okay, maybe it's dumb question, but have you saved your code change? - just in case... :) (Disclaimer: it happened to me...)
     
  11. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    352
    Yup saved the new code :)

    Also figured out the issue, the code for the part that the inspector was showing 5 gets variables from another script, and It was that script I kept checking to make sure that I never set it to 5, but i forgot I had changed the variables in the inspector for that script. :oops::oops::oops:
     
    Lurking-Ninja likes this.
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, glad you figured it out. :)