Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Changing an object into another

Discussion in 'Scripting' started by TheWgames, Aug 31, 2013.

  1. TheWgames

    TheWgames

    Joined:
    Nov 29, 2012
    Posts:
    27
    I want to know how to change an object from the hierarchy into another object.
    For example,
    object_A is changed into object_B when it touches object_C.
    And when enter is pressed, object_B changes into object_A again.

    What is the script of this?
    Does object_B has to be in the hierarchy or where?

    Thanks,
    Willie
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    what do you mean by object? a certain prefab, a script/class/type?
    usually you disable/destroy/despawn object a and instantiate/spawn object b on a's position. if a and b are of same prefab you can copy/set the values you want to be changed.
     
  3. TheWgames

    TheWgames

    Joined:
    Nov 29, 2012
    Posts:
    27
    There are two prefabs.
    A is a 3rd person controller with a camera parented.
    B is a car with a camera parented.
    I am not a very good script writer, so maybe you could write a script for me?
     
  4. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    if you let others do it for you this will never change. learning and experience comes from practice.

    sure i could but why would i? if you have a specific question/issue there are plenty of people here to help you. but "do it for me" will hopefully be ignored. its a pitty that this forum does not allow to "mark"/block leechers
     
  5. JBYT_LoadedTommy

    JBYT_LoadedTommy

    Joined:
    Sep 22, 2016
    Posts:
    1
    The point of this forum is to find and give answers, so let's keep any unrelated content out of.it.

    I need to figure out how to "change" objects with a script. I have a similar situation where I have an object (the player) that I want to transform into a ball (a sphere), and I don't know how to do this. I've looked at the Unity Manual, but I haven't figured out anything. Anyone know a way to do this?
     
  6. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Your "transformation" is a mere replacement of prefab. If you have put all your player controller stuff like inputs and camera control in a single object then you have fundamental problems since the character for games allowing object replacement or change of controlled object probably are made in pieces. The camera would have a controller which can be detached and re-attached to other objects.

    1) The camera controller detaches from the object to be removed
    2) Instantiate or awake the new object in the position and rotation of the object to be removed
    3) Remove or deactivate the object to be removed
    4) Camera controller attached to new object

    Think about RTSs where you can select individual units and command them. It's the same concept applied differently.

    We are not here to merely give answers and solutions to anyone who asks a question. We are not obligated to. The answer a user gives is therefore whatever their goal is. for example, my goal in answering is to get to the core of the problem rather than give a surface deep answer. A surface deep answer is merely a band-aid that will eventually break when it can't bear additional pressure.

    In this case, getting to the core of a problem would mean, have you structured your project scripts in a way that allows for this object switching feature. No? Then you better go back to the drawing board because any further work on your existing project would be grueling at best.
     
    Last edited: Sep 22, 2016
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    @Laperen, You might want to look at the date of what you're arguing with.

    Also holy hell can we put in a thing to prevent users with less than 10 posts from necroing?
     
  8. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    My bad, didn't take notice of the OP's post date. Although my point towards the necro-er stands. Which brings up another point:
    @JBYT_LoadedTommy , Start your own damn thread if you want to ask a question.