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

add an object as a child problem

Discussion in 'Scripting' started by prsn, Nov 12, 2014.

  1. prsn

    prsn

    Joined:
    Nov 2, 2014
    Posts:
    10
    Hi
    I have a moving object containing some objects.
    When i'm creating some of the child objects and setting the parent to parent object, the created object does not move with parent.
    I checked the hierarchy panel. but the created objects are not in the parents as a child.

    this is my code:
    GameObject parent = GameObject.Find ("Ground");
    clone.transform.parent = parent.transform;

    what is the problem
     
  2. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    Have you tried verifying that parent is being assigned correctly?
     
  3. prsn

    prsn

    Joined:
    Nov 2, 2014
    Posts:
    10
    yes.

    Here is complete function

    void OnTriggerExit2D(Collider2D other)
    {
    GameObject clone;
    clone = Instantiate(other,
    new Vector2(other.transform.position.x + (other.transform.lossyScale.x / 20),other.transform.position.y),
    other.transform.rotation) as GameObject;
    //clone.rigidbody2D.velocity = -Vector2.right * 5;
    GameObject parentObject = GameObject.Find ("Ground");
    clone.transform.parent = parentObject.transform;
    //print (parent.name);
    }
     
  4. arthur_fukushima

    arthur_fukushima

    Joined:
    Nov 12, 2014
    Posts:
    28
    May sound like a stupid question, but:

    Does this "Clone" object, contains a rigidbody?

    If yes, probably this is the problem. The parent moves, but the child still has his own physics.
     
  5. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    It would still be parented to the correct transform, though, I think.

    I think I'm missing something as I can't see an issue with that code. I would suggest running the Find() function in Start() or Awake() and add the parentObject as a member variable, though. There's no point in having to find the object over and over again if it's always going to be referencing the same object. I'd also use:
    Code (CSharp):
    1. Transform parentObject = GameObject.Find ("Ground").transfom;
    2. clone.transform.parent = parentObject;
    Unless there's a reason for you to reference the gameObject itself.

    The full script might be helpful, unless someone can spot something I'm missing.
     
  6. prsn

    prsn

    Joined:
    Nov 2, 2014
    Posts:
    10
    I did what you said but the problem still exists.
    Here is my complete script after fixing small problems.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnterTriggerControl : MonoBehaviour {
    5.  
    6.     Transform parentObject;
    7.  
    8.     void Start(){
    9.         parentObject = GameObject.Find ("Ground").transform;
    10.     }
    11.  
    12.     void OnTriggerExit2D(Collider2D other){
    13.         GameObject clone;
    14.         clone = Instantiate(other,
    15.                             new Vector2(other.transform.position.x + (other.transform.lossyScale.x / 20),other.transform.position.y),
    16.                             other.transform.rotation) as GameObject;
    17.         clone.transform.parent = parentObject;
    18.  
    19.     }
    20. }
    Yes the child has rigidbody 2D. But parent Does not.

    Here is my Hierarchy window after creating object.
    problem.jpg
     
  7. arthur_fukushima

    arthur_fukushima

    Joined:
    Nov 12, 2014
    Posts:
    28
    Sorry, I misunderstand the problem, so, when an object enters in your trigger you create another instance of this object?

    Don't know if this may be the problem (didn't tested...), but try to replace this line:

    Code (CSharp):
    1. clone = Instantiate(other,
    2. new Vector2(other.transform.position.x + (other.transform.lossyScale.x / 20),other.transform.position.y),
    3.                             other.transform.rotation) as GameObject;
    For this:

    Code (CSharp):
    1. clone = Instantiate(other.gameObject,
    2.                             new Vector2(other.transform.position.x + (other.transform.lossyScale.x / 20),other.transform.position.y),
    3.                             other.transform.rotation) as GameObject;
    (Instead of passing a Collider, use the GameObject)
     
  8. prsn

    prsn

    Joined:
    Nov 2, 2014
    Posts:
    10

    I don't know why, but this fixed the problem. Really thank you arthur_fukushima
     
  9. arthur_fukushima

    arthur_fukushima

    Joined:
    Nov 12, 2014
    Posts:
    28
    No problem :)

    I think that's because you were trying to create a collider, the correct is to create a new gameobject.