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 ICHeeryI · Sep 03, 2013 at 11:48 AM · changevaraccessingother

is this correct way to change var from other script?

Hello, is this correct way to change var from other script?

 //Health.js 
 static var lives : int =10;

Then, in other script:

 //Enemy.js
 Health.lives -= 1;

-Thanks

Comment
Add comment · Show 13
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 ZenithCode · Sep 03, 2013 at 11:58 AM 0
Share

Looks good to me. Does it work?

avatar image DeveshPandey · Sep 03, 2013 at 12:01 PM 0
Share

You should use PlayerPref for this purpose.

avatar image ICHeeryI · Sep 03, 2013 at 12:11 PM 0
Share

yes it works. for me.

avatar image dorpeleg · Sep 03, 2013 at 12:15 PM 2
Share

@DeveshPandey

He doesn't have to use PlayerPref.

PlayerPref is used to "save".

If he doesn't want to save his data to be persistent between play sessions then he doesn't need it.

@ICHeeryI

You might want to use GameObject.GetComponent

avatar image Fattie · Sep 03, 2013 at 01:07 PM 0
Share

Very generally, never use static

It is very confusing to new programmers. Just don't use it.

Show more comments

1 Reply

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

Answer by oferei · Sep 03, 2013 at 12:27 PM

Technically, yes, that would work - if you're working with static variables. But the power of object-oriented programming comes from instantiation (having multiple copies of the class in memory.)

By creating a Health.js script you actually defined a class called Health which extends MonoBehaviour. In your example it has a static variable, lives. To work with instances you should:

  1. Remove the static keyword

  2. Add the public keyword instead, so it is accessible from other classes

  3. Instantiate the class by attaching the script to a game object in the scene

Now, in order to access lives you need to have a reference to the game object. For example, let's say you attached the script to the object Enemy. There are various ways to get a reference to this object. You can set it up in advance by adding a public variable (of type Health) to the calling script and drag-and-dropping the Enemy object to it. Or you can get a reference during gameplay, for example in a collision between a weapon and the Enemy game object. You can find it by name, etc.

Once you have a reference to the game object, call that object's GetComponent method:

var enemyHealth : Health = enemyGameObject.GetComponent(Health);
enemyHealth.lives -= 1;

There, done.

Now to make things a little more complicated, you could leave the lives variable private and instead add a public method that would access it, for example, DoDamage. This method could decrease lives by one, and any other script can call DoDamage:

var enemyHealth : Health = enemyGameObject.GetComponent(Health);
enemyHealth.DoDamage();

This is better because it encapsulates the inner works of the Health class - nobody needs to know how to handle lives or what to do when it reaches zero. You also get the benefit of using Unity methods such as SendMessage, SendMessageUpwards and BroadcastMessage, which call a game object's method by name, without needing to know which class. For example:

enemyGameObject.SendMessage("DoDamage");

Note that these methods are easy to use, but are slightly slower.

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 Fattie · Sep 03, 2013 at 01:08 PM 0
Share

a great answer.

avatar image oferei · Sep 03, 2013 at 02:49 PM 0
Share

thanks mate.

avatar image ICHeeryI · Sep 03, 2013 at 03:25 PM 0
Share

From now on, I'll use this method. 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

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

Related Questions

Can't get var from other script. 1 Answer

Problem with accessing variable form other scripts. 1 Answer

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

The variables and functions 2 Answers

accessing a variable from another script. 3 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