Search Unity

call object from other objects child

Discussion in 'Scripting' started by grooc, Sep 5, 2011.

  1. grooc

    grooc

    Joined:
    May 23, 2011
    Posts:
    15
    Hello i have the following problem:

    In this hirachy, i want to call a function of the script "ShapeControll" attached to "gameObject2" from the child "child1".
    gameobject1 (instantiated)
    -child1(instantiated)
    -child2(instantiated)
    gameobject2


    script TriggerChoose is attached to child1 (and2) so it has to react on a MouseOver or OnMouseEnter event.
    script ShapeControll is attached to gameObject2 and has several functions.
    here i instantiate the Collider from a prefab which is supposed to become a child of gameobject1 ( c.transform.parent = gameobject1.transform;)

    the problem is, that this does not work, if the parent of the instantiated object is Gameobject1, however it does work perfectly when i use (c.transform.parent = transform;) instead.
    but this way, the instantiated objects dont behave like the rigidbody attached to gameobject1


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TriggerChoose : MonoBehaviour {
    5.    
    6.     public GameObject controll;
    7.     public int shape;
    8.  
    9.     void OnMouseEnter() {
    10.         controll.GetComponent<ShapeControll>().switchShape(shape);
    11.     }
    12. }
    13.  
    14. //some functions from the ShapeControll script
    15. using UnityEngine;
    16. using System.Collections;
    17.  
    18. public class ShapeControll: MonoBehaviour {
    19.     public void switchShape(int shape){
    20.         Debug.Log("here");
    21.     }
    22.  
    23.     void initiateCollider(int j){
    24.         Vector3 dposition = gameobject1.transform.position+radialPositions[j] ;
    25.         //Vector3 dposition = radialPositions[j] ;
    26.         GameObject c = (GameObject) Instantiate (colliderPrefab, dposition,  Quaternion.identity);
    27.         c.GetComponent<TriggerChoose>().shape = j;
    28.         c.GetComponent<TriggerChoose>().controll = this.gameObject;
    29.         c.transform.parent = transform;
    30.     }

    I have the feeling, that the MouseOver or OnMouseEnter functions are not called. Do you have any suggestions?
     
  2. grooc

    grooc

    Joined:
    May 23, 2011
    Posts:
    15
    does nobody have an idea? what is the problem that this does not work? or do you need more information to solve this?