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 AugustInSitu · Jul 20, 2016 at 01:33 AM · instantiateprefabstransform.positionsave datasaveload

Creating an instance of a prefab at start?

Hi, I am working on saving and loading gameobjects in my game. I am trying to save the players position and load it up at that particular transform position. I have everything working for objects that do not reference any scripts, but as soon as I try to move my player, I get a ton of errors saying that the player does not exist. I believe it is because of this line of code in particular (in my SaveGameManager script): public void Load () {

         foreach (SaveableObject obj in SaveableObjects)
         {
             if (obj != null)
             {
                 Destroy(obj.gameObject); //This is what destroys my gameobjects before re-instantiating them on 'load'
             }
 
         }

I have tried telling the Load function instead of destroying the gameobjects, to just move them (like this):

 transform.localPosition = obj.gameObject.transform.localPosition;

But this gives me an error relating to this like of code in my SaveGameManager (The error reads "NullReferenceException: Object reference not set to an instance of an object"):

             if (tmp != null) //if we have not already instantiated the object...
             {
                 tmp.GetComponent<SaveableObject>().LoadData(value); //instantiate the object
             }

("tmp" is an array of gameobjects that are 'saveable')

Anyone out there that can help me with this issue? I feel like it is right in front of my eyes - I'm just not seeing it.

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
0

Answer by DonkeyMalonkey · Jul 20, 2016 at 06:58 AM

So you what i am getting is that you have a player standing arround and you want to safe his position, so that on start you have the player right where he was, is that right?

If so it is probably easier to just use this kind of code:

 PlayerPrefs.SetFloat("lastXCoordinate", player.transform.position.x);
 
 PlayerPrefs.GetFloat("lastXCoordinate");

In my opionion if this is what you want it would be way easier to do it like that.

if you need to set the player on start you just set the transform.x to the PlayerPrefs.GetFloat("lastXCoordinate"), same with y and z of course.

If i am getting something wrong here what you are trying to do then just tell e right away.

hope this helps anyways!

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 AugustInSitu · Jul 20, 2016 at 08:56 AM 0
Share

Yes that is correct. However, I am using this system for all saveable objects and values in the game. I do realise I could be using PlayerPrefs (I actually am using them for saving game settings (resolution, etc.)), but for gameobjects that update during the game, I am using this system as it is more secure and flexible (also a good learning experience for instantiating and parsing).

All I need to know is how to instantiate a prefab (that references other scripts) to a saved position without having to ever destroy it - as this creates errors. Thanks for the input though!

avatar image DonkeyMalonkey AugustInSitu · Jul 21, 2016 at 10:30 AM 0
Share

I would really like to help, but it would seem i can´t on this one. Hope you get your answer soon!

geetrings Lucas.

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

73 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

Related Questions

How to get the position of a new Instantiated prefab 0 Answers

How do I instantiate particles properly,How do I instantiate particles correctly? 0 Answers

How to save money 1 Answer

Unity save gameobject list? (Save System) 1 Answer

Saving data of multiple instances of object? 0 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