Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Quick help

Discussion in 'Scripting' started by raffaga, Sep 1, 2015.

  1. raffaga

    raffaga

    Joined:
    Sep 1, 2015
    Posts:
    6
    I get this error in this script and I dont understand why??? or how to solve it.

    Error:
    ArgumentException: GetComponent requires that the requested component 'GameObject' derives from MonoBehaviour or Component or is an interface.
    UnityEngine.GameObject.GetComponentInChildren (System.Type type) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineGameObjectBindings.gen.cs:59)
    UnityEngine.GameObject.GetComponentInChildren[GameObject] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineGameObjectBindings.gen.cs:80)
    UnityStandardAssets.Characters.FirstPerson.ActionBarButtonDrag.Update () (at Assets/Scripts/ActionBarButtonDrag.cs:30)

    Code (CSharp):
    1. namespace UnityStandardAssets.Characters.FirstPerson{
    2.     public class ActionBarButtonDrag : MonoBehaviour, IDropHandler {
    3.  
    4.         public DragDrop dragDrop;
    5.         public InventoryV2 inventoryV2;
    6.         public ActionBar actionBar;
    7.         public Image itemIcon;
    8.         public int itemId;
    9.         public GameObject itemIconGO;
    10.         public Transform tempParent;
    11.         public GameObject erasedChild;
    12.  
    13.         private int tempID;
    14.  
    15.         // Use this for initialization
    16.         void Start () {
    17.  
    18.         }
    19.        
    20.         // Update is called once per frame
    21.         void Update () {
    22.             if(transform.childCount == 1){
    23.                 erasedChild = gameObject.GetComponentInChildren<GameObject>();
    24.             }
    25.        
    26.         }
    27. }
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    GameObject is not a component that you can get
    if you just want to get the child GameObject of some the GameObject, use transform.GetChild(0).gameObject
     
  3. raffaga

    raffaga

    Joined:
    Sep 1, 2015
    Posts:
    6
    Will that let me destroy the child?
     
  4. raffaga

    raffaga

    Joined:
    Sep 1, 2015
    Posts:
    6
    Anyone knows how I can get the child of an object and destroy it??
     
  5. goonter

    goonter

    Joined:
    Aug 31, 2015
    Posts:
    89
    Code (CSharp):
    1. foreach (Transform child in go.transform)
    2.         {
    3.             Destroy(child.gameObject);
    4.         }
    That will destroy all children. Sounds bad :)
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Did you not even try my solution? It will do what you want, and would take 30 seconds to try for yourself
     
  7. raffaga

    raffaga

    Joined:
    Sep 1, 2015
    Posts:
    6
    I not only tried your solution. I implemented the basic concept all over my code, for many other things. You help me make it more efficient. Sorry I forgot to thank you :))