Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 FlippingFlopper · Dec 24, 2019 at 02:30 AM · scriptingbasicsreferencegeneral programming

Better way of interacting and sharing variable between two scripts?

When I make a share of variable between two object (such as finding the player in playerJump script when the player is instantiated), I usually use Find.findgameobjectwithtag() method. By using this way all the times, it sometimes make things complicated. need to make exception for the object not exist, Find method takes too much system resources etc... so I thought there might be a better way that I do not know. is there any way to handle this issue?

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
1
Best Answer

Answer by xibanya · Dec 24, 2019 at 03:12 AM

Will there only ever be one player at a time? If so, at the top of your player class you could put

 public static Player instance;

and then in Awake()

 if (instance == null) instance = this;

let's say you're looking for player health. In the Player class you could have something like,

 public static bool TryGetHealth(out float health)
 {
 health = instance?.health ?? 0;
 return instance != null;
 }

usage in another script would be

 if (Player.TryGetHealth(out float playerHealth)) 
 {
 //do something with player health
 }
 else
 {
 //handle not being able to find the player this way (maybe now do the find by tag as a last ditch attempt)
 }

If you may have more than one player at a time, you will want to take another approach. One would be to give each player a unique ID and use a static Dictionary instead. You would need to add using System.Collections.Generic; to the top of the Player class file, then inside the class put public static Dictionary<int, Player> players = new Dictionary<int, Player>();

put public int id; in your variable declarations and manually assign it in the inspector OR add private static int playerCount; inside the class and increment it for each player created, then assign playerCount as the id.

In Start() put

 if (!players.ContainsKey(id)) players.Add(id, this);

and in OnDestroy() put

 if (players.ContainsKey(id)) players.Remove(id, this);

TryGetHealth would then look like this

 public static bool TryGetHealth(int id, out float health)
 {
 bool exists = players.ContainsKey(id);
 health = exists? players[id].health : 0;
 return exists;
 }
Comment
Add comment · Show 2 · 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 FlippingFlopper · Dec 24, 2019 at 06:46 AM 0
Share

I didn't even know that i could get the value with 'out' thing. ! Awesome!! thanks.

avatar image xibanya FlippingFlopper · Dec 24, 2019 at 06:49 AM 0
Share

Yep, it's pretty handy! Here's the official microsoft docs on it: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/out-parameter-modifier

ref is a similar and also very handy keyword for getting values out of a method without that necessarily being the return value https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref

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

127 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 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 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 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 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 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

Access functions in another script 4 Answers

How do I call a function in another gameObject's script? 5 Answers

Getting a " NullReferenceException: Object reference not set to an instance of an object " - Space Shooter Tutorial 14 - Counting Points 3 Answers

Call Functions Across Scripts, Null Object Error 1 Answer

Having an object reference itself 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