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 Shoon · Jan 18, 2014 at 10:29 AM · variablefunctionstatic

Static variables not working inside Function Update()

Hello ! Well, I have a big problem that I can't solve by any way and all I find on the internet is resolving the problem by the same way and it doesn't work for me.

If you get a static variable with the GetComponent stuff like explained in the Tutorial section of the website you have to put it inside Function Update() (if you don't, Unity calls an error). But it's okay, I went ahead of it. However, if now I want to use that variable inside a Function MonoBehaviour(), Function [any type of collision/trigger] or just somewhere else than inside Function Update(), the system shows an error.

I write some codes line right under to explain it beter.

 function Update() {
 
    var totems:Totem = GetComponent("Totem"); // Totem is how my other script is called
 }
 
 function OnCollionEnter(){
 
    if(totems.totem1==true){ // totem1 is a static boolean variable
 
       //Do Something
    }
 
 }

 
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

Answer by goo-muffin · Jan 18, 2014 at 10:46 AM

If you have a static variable then you will not need any instance of this script to call this static variable. lets say you got a non static variable in a script:

 Script.js
 
 var anthing :boolean;
 
 _______________________
 
 var sc :Script = GetComponent("Script");
 var anythingInThisScript :boolean = sc.anything;

if it is static the only thing you should do is to pack the variable in a class:

 Script.js
 
 class Totems
 {
  static var totem1 :boolean;
 }
 
 ------------------------
 
 anyScript.js
 
 function Update() //or any other
 {
   var totem1 :boolean = Totems.totem1; //no instance of this class or script is necessary
 }

I hope you understood it:

static variable -> no instance of this class or script. It is "static" and in each instance the same

non-static variable -> can be changed from instance to instance and is "non-static". You need to have an instance of the class/Script to get/change the variable

Comment
Add comment · Show 6 · 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 fafase · Jan 18, 2014 at 12:29 PM 1
Share

(Disclaimer, I am no believer and not trying to convert anyone)

Think of static like God, there is only one god for all human. Whether there is a human or not god is there and belong to all human. So you would make god static since it belongs to the human and not to a particular human. And you would access it via the Human class as Human.god ins$$anonymous$$d of via an instance of a human.

avatar image Shoon · Jan 18, 2014 at 01:41 PM 0
Share

Seems to work fine (let's see if it does too when I apply it to my full project). However I get one yellow error "The class defined in the script file named 's1' is not derived from $$anonymous$$onoBehaviour or ScriptableObject!", ¿ what does that mean exactly ?

avatar image Shoon · Jan 18, 2014 at 02:49 PM 0
Share

I used the class system for static variables you explained on my project and it suddenly called tons (like 15) errors about all identifiers I've set up declaring a variable of type GameObject for example and the dragging on the main screen the element I want to. However now it says something like "The associated script cannot be loaded, fix errors first to be able to compile it", so I cannot drag anything. The strange thing is that before changing all the static variables all worked well....

avatar image goo-muffin · Jan 19, 2014 at 09:20 AM 0
Share

well if it worked well then first thing I would do is to undo all the changes... change it back to how it looked before. Then you can try again with more concentration. Well my script should not be the solution! It was only an explination of the meaning of the word static (more or less).

avatar image Shoon · Jan 19, 2014 at 10:24 AM 0
Share

I know, but in fact, I've done it again and checked 10 times for errors and all seems correct, I don't know why the system don't recognize assignated GameObjects (and don't let me drag them in the main menu to maybe fix the errors......).

Show more comments

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

21 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

Related Questions

variable = true from another script 2 Answers

ERROR - An object reference is required to access non-static member 3 Answers

C# script for calculating the distance between the player and objects 1 Answer

Static Variable Problem 1 Answer

Public and Static Variables 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