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 Oct 01, 2013 at 10:55 PM by rednax20 for the following reason:

I Got My Answer

avatar image
0
Question by rednax20 · Sep 30, 2013 at 08:15 PM · gameobjectinspectorstatic variable

Static GameObject Variable

I have a quick question that i have wondered about for quite some time. You can normally set a static variable with the following code

 static var num = 20;

This works unless you have to drag an object (a gameobject, rigidbody, light, etc) into the inspector to assign it. for instance:

if you wanted to set your player variable you would code

 var player : GameObject;

This would work unless you wanted to make that player reachable throughout multiple scripts

 static var player : GameObject;

This doesn't show up in the inspector. Not that i need this for my project, I was just wondering if there was any way to have a static GameObject variable without taking up 3 lines to say :

 var player : GameObject; // the following is assigned in the inspector
 static var staticplayer : GameObject; //this is the static version of the variable
 staticplayer = player; // the static version now equals the version in the inspector

Simply a curiosity "What if" question. Thanks for taking the time to read this

Comment
Add comment · Show 1
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 · Sep 30, 2013 at 08:49 PM 0
Share

If you are using that field for dragging and dropping the gameobject, why would you need it to be a static variable?

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by clunk47 · Sep 30, 2013 at 09:00 PM

"var player : GameObject; This would work unless you wanted to make that player reachable throughout multiple scripts"

Is very untrue. To access something that is public but not static, use GetComponent. Say you have a script on your player with the Player variable set. Then you have some other script that wants to access the Player variable in the sense that it is not static. You need to have some reference to your object that has the player script on it, then access the script (component). I'm not sure why you want your objects to be static, but if you do,

1)They won't show up in the inspector.

2)There can only and always be ONE instance of the object.

3)It's easy to lose data if you're not certain on what you're doing with static variables.

The only time I really ever use static functions, classes, or variables is for Editor scripts.

Example on how to use GetComponent:

//PlayerScript.js

 var Player : GameObject;
 var num : int = 10;




//SomeOtherScript.js

 #pragma strict
 
 private var ObjectWithPlayerScript : GameObject;
 var playerScriptNum : int;
 
 function Start () 
 {
     ObjectWithPlayerScript = GameObject.Find("Player");
     //Say you want to change var num from PlayerScript:
     playerScriptNum = ObjectWithPlayerScript.GetComponent(PlayerScript).num;
     playerScriptNum += 10;
     print(playerScriptNum);
 }
 

Comment
Add comment · Show 3 · 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 rednax20 · Oct 01, 2013 at 01:24 AM 0
Share

Hey, hey, hey, don't get freaked out, It was just a hypothetical question, i was aware of GetComponent, i just wanted to know if there was an easier way to do it. I don't need to use it, but i just noticed that it could not be done without that much coding. I thought having a static GameObject could help when making generic scripts, you know to save time. It was just a quick question out of pure curiosity. Thank you for answering, I am going to take that as a "no".

Quishtay all the Way

avatar image clunk47 · Oct 01, 2013 at 10:52 PM 0
Share

So have you resolved your concern?

avatar image rednax20 · Oct 01, 2013 at 10:55 PM 0
Share

of course thank you for your concern

Follow this Question

Answers Answers and Comments

15 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

Related Questions

How to avoid storing the same resource for each gameobject? 1 Answer

Unity lag on selection 0 Answers

Assigning Assets to Static Variables 1 Answer

Position and rotation of a game object acting wired 0 Answers

"The associated script can not be loaded." called on every object in the scene 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