Search Unity

Cloning Problems

Discussion in 'Scripting' started by slippyd, Jun 16, 2005.

  1. slippyd

    slippyd

    Joined:
    Jun 11, 2005
    Posts:
    129
    The cloning is not working for me quite as expected. I have an inactive object exactly in the state that I want objects to be cloned from. When the first cloned object realizes its usefulness is up, it creates another clone from the inactive original. The problem is, every time they clone, their reference to the original changes to themselves. Thus, Block(clone) should have a reference to Block, like Block has a reference to Block, but instead Block(clone) has a reference to Block(clone) (itself), which is not very useful when trying to spawn objects off of the original. Is there a way around this and would it make sense for one of the options when choosing a GameObject reference to be "self"?

    Also, how can a script remove a script from itself or from an GameObject it has a reference to? And how can you call functions in or change values for a script in a GameObject you have a reference to? Thanks.
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    The cloning behaviour is exactly the same as the duplicate behaviour.

    The game object and transform children are cloned. The component attached to the game object are cloned too.

    All references between the components are resolved by this rule:
    If the old reference was cloned, the new reference will point to the clone.

    This is what you want in most cases.

    To solve your case you want to have a variable in the script that references the original game object and then manually set it after cloning.

    Code (csharp):
    1.  
    2. var original : GameObject;
    3. function OnMouseDown ()
    4. {
    5.     var clone : GameObject = Instantiate (original);
    6.     var cloneScript :  CloneScript = clone.GetComponent (CloneScript);
    7.     cloneScript.original = original;
    8. }
    9.  
    Currently there is no way to remove a component from scripting. I will add support for removing components in the 1.0.1 release.

    use GetComponent. See the example above. The name of the class is the name of the script.
     
  3. Vinayak-VC

    Vinayak-VC

    Joined:
    Apr 16, 2019
    Posts:
    60
    It's 2019.