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 /
  • Help Room /
avatar image
0
Question by ivarhill · Jan 13, 2017 at 05:13 PM · referenceclass instance

Best way to reference player class instance?

I'm making an FPS which has a "Player" class attached to a game object in a scene. This class keeps track of things like player health, ammo etc. and has functions for various player behavior.

I need to access this data frequently from other instantiated classes - a good example would be medkits spread across the levels. The way things are currently set up, the Player class has a SetPlayerHealth function which modifies the health of the player, and each pickup detects if collided with and calls this function in the instantiated Player class, passing along the amount of health to modify by.

This works (and I like having pickups and such detect the collision in their own class since it's more modular), but I'm struggling with figuring out the best way to actually refer to the instanced Player class.

I can make a "player" variable in the other classes and do this either when spawned or when needing to call the Player class:

 player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();

And then refer to this variable. But while this works well for objects there are just a few, set amounts of (say, UI elements) it seems badly optimized for things like pickups/medkits which continuously instantiate and there are many of - to my understanding finding game objects by tag at runtime isn't great for performance.

What would be the optimal/recommended way to refer to the instantiated Player class efficiently? (or is there a better approach to basic player structure altogether?)

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

Answer by tanoshimi · Jan 13, 2017 at 05:33 PM

The normal solution to this is to have a singleton "manager" object in your scene that keeps references to all the important things you might need - the player, the audiomanager, the save system, and then have everything access them through that.

Simple example follows:

 public class SceneManager : MonoBehaviour {

   // Declare any public variables that you want to be able 
   // to access throughout your scene
   public Player player;

   public static SceneManager Instance { get; private set; } // static singleton
   void Awake() {
         if (Instance == null) { Instance = this;  }
         else { Destroy(gameObject); }

         // Cache references to all desired variables
         player= FindObjectOfType<Player>();
     }

Now, from any other script you access the cached reference in the singleton, avoiding the need to do any expensive FindWIthTag()-esque lookups by simply looking at:

 SceneManager.Instance.player
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 ivarhill · Jan 13, 2017 at 10:43 PM 0
Share

Thanks for the reply! I haven't used singletons before, I'll definitely try and read into this.

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

60 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

Related Questions

component not detecting parent class 0 Answers

"Object reference not set to an instance of the object" yet I can see the reference is working in monodevelop 1 Answer

MonoDevelop Unity API reference function with wrong path. How to Fix it? 1 Answer

Create an array of scripts that don't affect each other? 1 Answer

Get reference to spawned object from server on client 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