- Home /
How do I replace an old gameObject with a new one while keeping the functionality of the old one?,How do I replace my current gameObject with a new one while maintaining the functionality of the old one?
I'm having some difficulty replacing the cube (current gameObject) with a completely new sphere. I've managed to destroy the cube and replace it with the sphere, but I seem to have lost all the functionality that the cube had via key presses (i.e. rotating, changing the colors, etc.). Is there a way to set the old cube methods to the new sphere's? (i.e. newSphere.transform.rend.material.SetColor() = this.transform.rend.material.SetColor())
/* How to Use Controls:
*
* CHANGING POSITION
* Press W to move the cube forwards
* Press S to move the cube backwards
* Press A to move the cube to the left
* Press D to move the cube to the right
* Press F to move the cube up
* Press V to move the cube down
*
* CHANGE ROTATION
* Press right arrow key to rotate cube to the right
* Press left arrow key to rotate cube to the left
* Press up arrow key to rotate cube upwards
* Press down arrow key to rotate cube downwards
*
* CHANGE SCALE
* Press 2 (on the top number pad) to double the size of the cube
*
* CHANGING COLORS
* Press R to change the cube to red
* Press G to change the cube to green
* Press B to change the cube to blue
*
* RESETTING ALL FEATURES
* Press 0 to reset all changes that you made
*
* Karen Lee
* CMPM 121
* 1/12/2019
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cubeControls : MonoBehaviour
{
// Constants for object rotation
public float moveSpeed = 80.0F;
public float turnSpeed = 100.0F;
// Initial scale of the original cube
public static Vector3 initscale = Vector3.one;
// Start is called before the first frame update
void Start()
{
Debug.Log("I am alive!");
}
// Update is called once per frame
void Update()
{
/* Changing the position of the object
*
*/
// Moving the object right
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
}
// Moving the object left
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
}
// Moving the object forwards
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);
}
// Moving the object backwards
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
// Moving the object up
if (Input.GetKey(KeyCode.F))
{
transform.Translate(Vector3.up * moveSpeed * Time.deltaTime);
}
// Moving the object down
if (Input.GetKey(KeyCode.V))
{
transform.Translate(Vector3.down * moveSpeed * Time.deltaTime);
}
/* Changing the rotation of the object
*
*/
// Reset the rotation to original by returning a rotation at (0, 0, 0)
if (Input.GetKey(KeyCode.Alpha0))
{
transform.rotation = Quaternion.Euler(Vector3.zero);
}
// Rotating the cube to the right
if (Input.GetKey(KeyCode.RightArrow))
{
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
}
// Rotating the cube to the left
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Rotate(Vector3.down, turnSpeed * Time.deltaTime);
}
// Rotating the cube in an upwards motion
if (Input.GetKey(KeyCode.UpArrow))
{
transform.Rotate(Vector3.right, turnSpeed * Time.deltaTime);
}
// Rotating the cube in a downwards motion
if (Input.GetKey(KeyCode.DownArrow))
{
transform.Rotate(Vector3.left, turnSpeed * Time.deltaTime);
}
// Saving the current rendered material
Renderer rend = GetComponent<Renderer>();
/* Changing the scale of the object
*
*/
// Reset the cube to the original scaled size
if (Input.GetKeyDown(KeyCode.Alpha0))
{
transform.localScale = initscale;
}
// Double the size of the cube
if (Input.GetKeyDown(KeyCode.Alpha2))
{
transform.localScale += new Vector3(2F, 2F, 2F);
}
/* Changing the color via key presses
*
*/
// Reset color of cube to original
if (Input.GetKeyDown(KeyCode.Alpha0))
{
rend.material.SetColor("_Color", Color.white);
}
if (Input.GetKeyDown(KeyCode.R))
{
rend.material.SetColor("_Color", Color.red);
}
if (Input.GetKeyDown(KeyCode.G))
{
rend.material.SetColor("_Color", Color.green);
}
if (Input.GetKeyDown(KeyCode.B))
{
rend.material.SetColor("_Color", Color.blue);
}
// Changing to sphere
if (Input.GetKeyDown(KeyCode.X))
{
GameObject newSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
Destroy(this.gameObject);
}
}
}
Does your new object have a cubeControls component on it?
Ah, I don't believe so. I think I see what you're saying, so I should use "AddComponent" to attach the cubeControls to the new gameObject?
That should do it. Another approach would be to save both the cube and sphere objects as prefabs, and instantiate them as needed.
Answer by badadam · Jan 13, 2019 at 09:00 PM
Create empty gameobject named mainObje for example. Add Cube and Sphere as a child of the empty gameobject. As I understand from your script, Cube is the first object and you want to change Cube with Sphere by pressing KeyCode.X. Make the sphere inactive. Uncheck Sphere checkBox . Add this code your script and you should add the script to parent object(I named mainObje as you can see the picture) of Cube and Sphere
public GameObject objeCube;
public GameObject objeSphere;
private void Awake()
{
objeCube = transform.Find("Cube").gameObject;
objeSphere = transform.Find("Sphere").gameObject;
}
private void Update()
{
//your other Update codes
if (Input.GetKeyDown(KeyCode.X))
{
objeCube.SetActive(false);
objeSphere.SetActive(true);
}
}
Answer by sh_code · Jan 13, 2019 at 08:28 PM
separate visuals and functionality. Make an object with this script of yours, but put the VISUAL REPRESENTATION of the object as a child of the parent.
Meaning, you will not be destroying and replacing the object with all of the functionality scripts, you'll be destroying its child which only has the cube mesh to be drawn, and putting a new one with a sphere mesh.
In general, it's not wise to have the visuals on the same gameobject as functionality. ALWAYS try to have the visuals (models/sprites/whatever) as a child object of the one containing the logic.
I usually go one step further, as an object's visuals can be comprised of more things (mesh, line renderer, particle system), so I have a Parent object which only contains the scripts, and as its child, I have an empty "visuals" object, and as its children, I have anything and everything that's actually being rendered.