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 reviyee · Apr 03, 2018 at 08:54 PM · gameobjectprefabprefabsinstance

Save a gameobject as prefab on button press.,Is there a way for the player to create a prefab with a button press?

Is there a way to save a prefab of an object at the press of a UI.Button?

I have a gameObject called gameManager which contains every instanced object on my game. I want to be able to save/load it at button press.

Is there a way to do so or are prefabs only create-able on Unity Editor?,I was thinking of a a save/load system that saves a whole gameObject called "GameManager" which contains everything about the game to a prefab, and then loading it if the player wishes to. Is there a way to let the user save a prefab with the push of a UI.Button?

Comment
Add comment · Show 1
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 losingisfun · Apr 04, 2018 at 02:00 AM 0
Share

Do you want to save data even when the game has been quit and restarted?

3 Replies

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

Answer by reviyee · Apr 04, 2018 at 04:43 PM

Thanks for everyone's time. I managed to select which GameObjects were enabled/disabled by getting every child object on the GameManager GO, saving a string array (1 if neabled, 0 if disabled) (using PlayerPrefs) and then loading the string, passing it to a int array and setting the state of the GO using said array.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Game_Manager : MonoBehaviour {
     // Stores a list of every gameObject as child on the GameManager gameObject.
     public List<GameObject> childList = new List<GameObject>();
 
     // Used to transform the PlayerPrefs Load string to an array.
     private char[] charArray;
 
     // Used together with CharArray to set 
     public List<int> isEnabledLoad = new List<int>();
 
     void Start()
     {
         // Adds every child of the gameManager to a gameObject list.
         foreach (Transform child in this.transform)
         {
             childList.Add(child.gameObject);
         }
     }
 
     public void Save()
     {
         // Creates a string to store gameObject Active state using PlayerPrefs.
         string enabledString = "";
 
         // If the child GO is disabled, concatenate a 1 to 'enabledString', else, concatenate a 0.
         foreach (GameObject child in childList)
         {
             if (child.activeInHierarchy)
                 enabledString += '1';
             else
                 enabledString += '0';
         }
 
         PlayerPrefs.SetString("savestate", enabledString);
         PlayerPrefs.Save();
     }
 
     public void Load()
     {
         // Transforms the Save string to a char array.
         charArray = PlayerPrefs.GetString("savestate").ToCharArray();
         isEnabledLoad.Clear();
 
         // For each char in charArray, parse the value to integer and add it to an Integer List.
         foreach(char c in charArray)
         {
             isEnabledLoad.Add(int.Parse(c.ToString()));
         }
 
         // Loops throught the Integer list and if the value is 0, disable the child GO.
         for (int i = 0; i < childList.Count; i++)
         {
             if(isEnabledLoad[i] == 0)
             {
                 childList[i].SetActive(false);
             }
         }
     }
 }
 
Comment
Add comment · 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
0

Answer by Ginxx009 · Apr 04, 2018 at 02:06 AM

This may help you a bit i guess.

 // Save the transform's GameObject as a prefab asset.
 PrefabUtility.CreatePrefab(prefabPath, trans.gameObject);

the trans.gameObject is the object you want to be the prefab

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 fffMalzbier · Apr 04, 2018 at 07:13 AM 0
Share

The PrefabUtility class is only available in the editor, so not usable at runtime.

avatar image
0

Answer by Laurentino · Apr 04, 2018 at 07:35 AM

What you need to do is:
a) Serialize your game object to be able to save/load it at runtime.
b) Write an editor utility to load your saved game object and make a prefab out of it
 
It's no easy task but you could probably find some plugins to help you with that.

Comment
Add comment · 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

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

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

Related Questions

Create with code 2.2 pizza not shooting 1 Answer

Affect All Instances Of A Prefab 3 Answers

Mark gameobject field as changed from prefab 2 Answers

How to tell at runtime if a GameObject is a prefab 3 Answers

How to instantiate multiple Prefabs to multiple Childs randomly ? 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