- Home /
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;
}
}
}
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 >);
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;
}
}
}
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.
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;
}
}
}
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?
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
Follow this Question
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