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 klee57 · Jan 13, 2019 at 07:28 PM · rotationtransformobjectscalecolor change

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);
             }
         }
     }
 
Comment
Add comment · Show 4
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 Bonfire-Boy · Jan 13, 2019 at 07:39 PM 0
Share

Does your new object have a cubeControls component on it?

avatar image klee57 Bonfire-Boy · Jan 13, 2019 at 08:18 PM 0
Share

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?

avatar image Bonfire-Boy klee57 · Jan 13, 2019 at 08:29 PM 0
Share

That should do it. Another approach would be to save both the cube and sphere objects as prefabs, and instantiate them as needed.

Show more comments

2 Replies

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

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. alt text 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 alt text. 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);
           }
      }
 


ekran1.png (1.7 kB)
ekran2.png (29.8 kB)
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 sh_code · Jan 13, 2019 at 08:28 PM

  1. 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.

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

154 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 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

I'm interpolating an object between positions and rotations. Now I need to make sure it happens in front of the camera. 1 Answer

[solved] Get upward orientation of object and add scale value 1 Answer

postion scale and rotation greyed out and not working 1 Answer

How to turn the steering wheel 0 Answers

GUI item with object position+dimensions 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