Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Ochreous · Jun 22, 2013 at 06:27 AM · c#gameobjectrevert

C# Reverting GameObject to Original

Hi everyone, I have script that changes someGameObject into SwitchOut. Now I want to make it so I can change it back to what it was. I've created preservedGameObject to preserve someGameObject. Now when I run my script and click change preservedGameObject says it's missing a gameobject. I assume that's because I destroyed someGameObject. Is there a way I can change it back to what it was?

 using UnityEngine;
 using System.Collections;
 
 public class SomeScript : MonoBehaviour {
 public string[] someStringArray;
 public string someString;
 public GameObject switchOut;
 public GameObject someGameObject;
 public GameObject preservedGameObject;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void OnGUI () {
     if(GUILayout.Button("Change", GUILayout.Width(75), GUILayout.Height(25))){
     Vector3 position = someGameObject.transform.position;
     Quaternion rotation = someGameObject.transform.rotation;
     preservedGameObject = someGameObject.gameObject;
     Destroy(someGameObject);
     someGameObject = Instantiate (switchOut, position, rotation) as GameObject;
         }
     if(GUILayout.Button("Revert", GUILayout.Width(75), GUILayout.Height(25))){
     Vector3 position = someGameObject.transform.position;
     Quaternion rotation = someGameObject.transform.rotation;
     Destroy(someGameObject);
     someGameObject = Instantiate (preservedGameObject, position, rotation) as GameObject;
         }
     }
 }
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 iwaldrop · Jun 22, 2013 at 06:25 PM

You likely want to simply set the preserved game object to inactive. When you want to use it again, just set it to active and update its position and rotation if necessary.

 gameObject.SetActive(< true or false >);
Comment
Add comment · Show 5 · 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 Ochreous · Jun 22, 2013 at 06:58 PM 0
Share

It still says preservedGameObject is missing a GameObject.

 using UnityEngine;
 using System.Collections;
 
 public class SomeScript : $$anonymous$$onoBehaviour {
 public string[] someStringArray;
 public string someString;
 public GameObject switchOut;
 public GameObject someGameObject;
 public GameObject preservedGameObject;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void OnGUI () {
     if(GUILayout.Button("Change", GUILayout.Width(75), GUILayout.Height(25))){
     Vector3 position = someGameObject.transform.position;
     Quaternion rotation = someGameObject.transform.rotation;
     preservedGameObject = someGameObject.gameObject;
     preservedGameObject.SetActive(false);
     Destroy(someGameObject);
     someGameObject = Instantiate (switchOut, position, rotation) as GameObject;
         }
     if(GUILayout.Button("Revert", GUILayout.Width(75), GUILayout.Height(25))){
     Vector3 position = someGameObject.transform.position;
     Quaternion rotation = someGameObject.transform.rotation;
     Destroy(someGameObject);
     preservedGameObject.SetActive(true);        
     //someGameObject = Instantiate (preservedGameObject, position, rotation) as GameObject;
         }
     }
 }
avatar image iwaldrop · Jun 22, 2013 at 07:52 PM 0
Share

Right, because GameObjects are reference types.

What you're telling Unity is that preservedGameObject is someGameObject. So when you destroy someGameObject you're destroying the reference for preservedGameObject. Basically, don't reassign preservedGameObject to someGameObject and it'll work. PreservedGameObject should be whatever the old gameObject is, not what the temporary one is.

So here, you'll want to simply disable your GameObject, instantiate your switchOut object, then destroy (or disable) switchOut and enable the original GO.

avatar image Ochreous · Jun 22, 2013 at 08:10 PM 0
Share

I got it to change and revert once. Then for some reason it changes switchOut and doesn't do anything with someGameObject afterwards. Also how do I set the preservedGameObject so it equals what someGameObject was previously?

 using UnityEngine;
 using System.Collections;
 
 public class SomeScript : $$anonymous$$onoBehaviour {
 public string[] someStringArray;
 public string someString;
 public GameObject switchOut;
 public GameObject someGameObject;
 public GameObject preservedGameObject;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void OnGUI () {
     if(GUILayout.Button("Change", GUILayout.Width(75), GUILayout.Height(25))){
     Vector3 position = someGameObject.transform.position;
     Quaternion rotation = someGameObject.transform.rotation;
     preservedGameObject = someGameObject.gameObject;
     preservedGameObject.SetActive(false);
     switchOut.SetActive(true);
     someGameObject = Instantiate (switchOut, position, rotation) as GameObject;
         }
     if(GUILayout.Button("Revert", GUILayout.Width(75), GUILayout.Height(25))){
     Vector3 position = someGameObject.transform.position;
     Quaternion rotation = someGameObject.transform.rotation;
     preservedGameObject.SetActive(true);
     switchOut.SetActive(false);
     someGameObject = Instantiate (preservedGameObject, position, rotation) as GameObject;
         }
     }
 }
avatar image Ochreous · Jun 22, 2013 at 09:32 PM 0
Share

It turned out that the object just kept getting instantiated and I wasn't seeing it change but it did indeed change. It also keeps instantiating as I click. How can I control how many objects get instantiated?

avatar image iwaldrop · Jun 22, 2013 at 11:22 PM 0
Share

Use a flag or a counter. When you click, set the flag to true or increment the counter. Check the flag or counter before instantiating it for a valid value.

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

16 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

Related Questions

C# Adding Components From Other Gameobjects 3 Answers

Destroy + Score 1 Answer

C# Raycast 2D GameObject follow Mouse 1 Answer

C# SetActive GameObject Array 2 Answers

C# Rotate GameObjects Regardless of List Size 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