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 Johan 4 · Apr 16, 2011 at 10:40 PM · gameobjectvalueaccess

Alternative way of accessing other scripts?

I have a "global" script that contains all the variables and settings and such.

I have an object that is removed after X seconds. The x seconds is defined in the global script.

Right now I do it like this:

public globalscript gls;

and then

gls = gameObject.Find("GlobalHolder").GetComponent(globalscript);

Replaced sharp parenthesis because they won't show to find the script. I have a gameobject in the world with that name that has the global script attached.

(that's in c#)

What I want to know if this is the way to do it or is there another way? Because right now I need this piece of code in every single script because they all read some value from the global script.

Thanks

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 Goody! · Apr 17, 2011 at 01:49 AM 0
Share

I'm not the best coder but your approach seems reasonable.

2 Replies

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

Answer by Uzquiano · Apr 17, 2011 at 10:43 AM

Well... yes, if you are newie with OO programing can be hard. But I will write you one example and then you can decide :)

So if you have a global variable in your 'GlobalHolder' to keep your score, you can define it like this: NOTE that I am writing JavaScript but the diferences are minimum or any.

static var score : int;

Then in whatever script you can access your 'score' like this:

GlobalHolder.score = 2; //this is an example of access

So as you can see there is no need of make an instance of GlobalHolder, which means that there is only one instance... if you are acessing very very very often to a static varible or if the functions you are accessing static variables are very very very long you might have race conditions. But simplifying, in Unity I didn't have those kind of problems... yet ;)

Hope you can solve your stuff

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
avatar image
0

Answer by Uzquiano · Apr 16, 2011 at 11:20 PM

Hi Johan,

Have you considered use static variables in your global script?

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 Johan 4 · Apr 17, 2011 at 12:02 AM 0
Share

I did consider that, yes. I am kinda noobish to c# and program$$anonymous$$g overall and I have no idea what static actually does. I tried reading on google, but my puny $$anonymous$$d couldn't really understand what it does. Yes I can use global.blabla but where does it pull this from? If the script is not in the scene, how can it access it? It's a very confusing beast.

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

No one has followed this question yet.

Related Questions

How to access gameObject variable script 2 Answers

Get GameObject name from other object. 2 Answers

How can I access other scripts and their functions? 3 Answers

How do you access a script if you don't know it's name? 5 Answers

Access a class on another script? 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