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
  4. Dismiss Notice

Question Alternatives to Instantiate to copy or 'clone' gameObject?

Discussion in 'Scripting' started by eco_bach, Feb 17, 2021.

  1. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Can anyone tell me is using Instantiate
    https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
    the ONLY way to duplicate or clone a game object in your scene?

    I need to dynamically create 'walls' from a floor plane in my scene.
    The easiest way would be to simply duplicate or clone my floor, offset in either x or z direction, then rotate 90 degrees.
    However duplicating an object in this fashion doesn't seem possible.
    Anyone?
     
  2. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    The answer is yes, but why do you think that that means you can't clone your floor and offset it? That's exactly what Instantiate does.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Totally possible. Do it, and when it fails, press pause and look in the scene to find out what went wrong.

    Is the wall actually in the right place after all?

    Are you perhaps not using the right offsets to the new position? Fix that.

    Do you perhaps have a root animation driving the object back to somewhere? Remove that!

    etc. etc.

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
    BarriaKarl and eco_bach like this.
  4. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Thanks. Well thought out response.
     
    Kurt-Dekker likes this.
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,923
    If there are a fixed number of possible new walls, in preset positions, it's often easier to create them all, leaving them inactive until needed.

    Otherwise, to be clear, you almost never want to Instantiate from a gameObject in the scene. Your floor may have had it's color changed, or whatever else. It's safer to make a prefab and Instantiate that.
     
    BarriaKarl likes this.
  6. AdamDice

    AdamDice

    Joined:
    Jun 20, 2022
    Posts:
    7
    I have the same issue, I want to use a prefab as a template for a game object but it's a reference