Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 uri2563 · Oct 23, 2019 at 07:56 PM · singletonpointerclass instance

singleton pointer and updating variables of the singleton with the pointer

When i have created a player class and created a singleton in it:

public static Player instance;

 void Awake()
 {
     if (instance != null)
     {
         Debug.LogWarning("More than one instance of Inventory found");
         return;
     }
     instance = this;
 }


one variable in Player class is playerData and in player data where is a variabale inventory (array of items). in the inventoryUpdate scrip i want to change the items in the inventory that is in the player class . I don't want to call the class every time i want to change (not to call Player.playerData.inventory every time) I tried to point on the inventory variable and call it on Create local variable inventory and that will point on inventory in Start(){inventory = Player.playerData.inventory;} but the playerData.inventoryin in the player class is not changing when i try to change it on the inventoryUpdate with the local pointer inventory: inentory[0] = newItem("sword") and it is changing only the local pointer and not the original variable. I thought that if i never defined a new instance (never used new word) it is only a pointer and it will change the original variable. is there any way to do it? defined a pointer correctly?

Comment
Add comment · Show 3
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 Pangamini · Oct 24, 2019 at 08:26 AM 0
Share

Can we see your code?

avatar image xxmariofer · Oct 24, 2019 at 08:46 AM 0
Share

what type of variable is inventoryin?

avatar image Bonfire-Boy · Oct 24, 2019 at 11:54 AM 0
Share

It's far from clear that the inventory issue is anything at all to do with the singleton. Like the others have said, you need to show us the code that relates to the inventory object. What is it? How and when are you creating/modifying it? (both the actual object and the cached pointer to it)

Having said that, the singleton use does look odd from what you've shown. You're accessing playerData as if it's a static property of the Player class. You've not shown us anywhere that you're actually accessing the singleton (ie via Player.instance). So, again, there's nothing to connect the code or the question title with the problem you're describing.

0 Replies

· Add your reply
  • Sort: 

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

116 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

Related Questions

Saving last checkpoint hit when reloading scene - Singletons? 1 Answer

singleton, static var, local reference. Whats better? 1 Answer

Create a persistent GameObject using Singleton 3 Answers

Text UI erases after new scene. 1 Answer

Coroutine stops after while-loop 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