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

GetComponent<Script>(true) gives error

Discussion in 'Scripting' started by Stonewood1612, Feb 21, 2015.

  1. Stonewood1612

    Stonewood1612

    Joined:
    Sep 15, 2014
    Posts:
    32
    Hello Unity community, I'm stuck again.

    So I'm trying for an if-statement to check if a gameobject has a certain script, but now I also need it to check inactive objects. My original (functioning) piece was:

    hit.collider.gameObject.GetComponentInChildren<TurretMain> ()

    So that doesn't check inactive children. I went searching a bit and found out that you can set the includeinactive parameter to true, like this:

    hit.collider.gameObject.GetComponentInChildren<TurretMain> (true)

    Unfortunately, that gives the following error:

    error CS0308: The non-generic method `UnityEngine.GameObject.GetComponentInChildren(System.Type)' cannot be used with the type arguments

    Crap, on the examples I found they said it worked. I've been searching for a while now and I can't seem to find a similar case. How do I make this work?
     
  2. Black-pearl

    Black-pearl

    Joined:
    Mar 20, 2014
    Posts:
    55
    try this maybe
    Code (csharp):
    1.  
    2. if(hit.collider.gameObject.GetComponentInChildren<TurretMain>().active == true)
    3. {
    4.    hit.collider.gameObject.GetComponentInChildren<TurretMain>().active = false;
    5. }
    6.  
    Something like that might work
    *Not tested

    :)
     
  3. Stonewood1612

    Stonewood1612

    Joined:
    Sep 15, 2014
    Posts:
    32
    I doubt that would work. Only a gameobject can be active or not. A component can be disabled and enabled.

    But even then when a gameobject is SetActive(false), the components are still enabled. :/
     
  4. Black-pearl

    Black-pearl

    Joined:
    Mar 20, 2014
    Posts:
    55
    ah yeh maybe change .active for .enabled?

    Very good point lolz

    EDIT ** or you could change it to a transform and use Getchild?

    so say

    gameobject.transform.GetChild(1).GetComponent<Script>().enabled == false;

    How about that?
     
  5. Stonewood1612

    Stonewood1612

    Joined:
    Sep 15, 2014
    Posts:
    32
    Yeah I tried using enabled, fixes the compiler error, but gives null errors at runtime.

    Does GetChild work when the parent has multiple children? I may be able access the child object with a tag though.
     
  6. Black-pearl

    Black-pearl

    Joined:
    Mar 20, 2014
    Posts:
    55
    yes so for example: -

    GetChild(0) - is the first child
    GetChild(1) - is the next child

    Always have 0 as the first child that might be why you get the null exception because its looking for something thats not there :)
     
  7. Stonewood1612

    Stonewood1612

    Joined:
    Sep 15, 2014
    Posts:
    32
    Ok that may be very well possible, the child I'm looking for would be number 3. I hope when I in the future instantiate a new child under a parent that doesn't have one yet it would become number 3 as well.

    Edit: it works with that method.

    Edit2: Damn, at runtime it messes up the hierarchy, so the child I'm looking for isn't always number 3. I'll have to do a foreach loop I guess?
     
  8. Stonewood1612

    Stonewood1612

    Joined:
    Sep 15, 2014
    Posts:
    32
    Got it working.

    Code (CSharp):
    1. foreach(Transform child in controledPlatform.transform){
    2.                         Debug.Log("Got child");
    3.                         if(child.gameObject.tag == "Destroyed"){
    4.                    
    5.                             Debug.Log ("Got right one");
    6.                             return;
    7.                         }
    8.                     }
    I check this after the if-statement before doing anything else. I can work with this. Thanks.