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 moonLite · Oct 29, 2012 at 01:52 PM · booleandeclare

why declare boolean value before functions didnt work?

Hi Guys,

The clockIsPaused (boolean type) only returns true if I declare it in the function. But why at the top or before the function, it didn't work? I thought we can input all the values when we declare the var which type and before all function start?
(I know it's sounds silly,that function is where all the process of code start) I'm just curious.

 #pragma strict
 
 var clockIsPaused : boolean = true;
 function Start () 
 {
     //clockIsPaused = true;// even if I put the boo lean in the declare top it,
 //it is still as false, why?
     }
 
 function Update () {
     Debug.Log(clockIsPaused);
 }
Comment
Add comment
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

1 Reply

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

Answer by Paulius-Liekis · Oct 29, 2012 at 02:13 PM

Because it is automatically public and it is set to false in on our GameObject. Once the script is attached to GameObject values are taken from GameObject (in the inspector), not from declaration.

Solution: make it private. Or change it on start/awake.

Comment
Add comment · Show 4 · 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 moonLite · Oct 29, 2012 at 02:22 PM 0
Share

Thanks Paulius,
can I assume that when all public var is null or false when its script is attached to GameObject? (if we didn't key in any value in the Inspector)

avatar image Paulius-Liekis · Oct 29, 2012 at 02:51 PM 0
Share

Yes and no. It will take default value or value that you defined in the declaration. So if you have declaration "var myInt : int", then value will be 0 (because default for int is 0), but if you have "var myInt : int = 5", then 5 will be taken as initial value when you attach the script.

avatar image moonLite · Oct 29, 2012 at 03:14 PM 0
Share

It's weird, I removed my script, And edited and attach my script again.

This time, my new var booleans and var Integer's values all shown correctly to what I declared on the top. ( I declared 2 booleans, one true one false & one int : 99)

But didn't you say default value is zero & I experienced that it didn't shown correctly even i declared on the top.
1) How come it's all returning correct value now?

2) back to what you were saying, if assu$$anonymous$$g I didn't encounter the problems I said now, for lets say float or int values, it will return correctly of what I declared on the top even it's public, right?

avatar image moonLite · Oct 29, 2012 at 03:20 PM 0
Share

I realize, if i Removed it or reset it, the values will return correctly (the top declare vars)

3) So the Inspector values of var is always null(except int), until I reset or remove & attach the script again?

Please let me know if I get it right, thanks.

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

10 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

Related Questions

Boolean value returns both true and false 1 Answer

Randomly setting a boolean to true? 3 Answers

Can't translate bool from C# to unityscript 2 Answers

Performance Question, Change Boolean or Enable-Disable Script 0 Answers

Open and close doors by pressing 'E' 2 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