Search Unity

GetComponent<>() references are pointing to parent asset in project hierarchy, not the root asset.

Discussion in 'Scripting' started by IllTemperedTunas, Oct 9, 2019.

  1. IllTemperedTunas

    IllTemperedTunas

    Joined:
    Aug 31, 2012
    Posts:
    782
    I'm attempting to expose the abilities of a given NPC and lock the window so I have easy access to them in the editor. Problem is, when I click the referenced script derived from GetComponent, instead of taking me to the ability in the project hierarchy it attempts to take me to the parent gameobject as far as I can tell.
    Here's the code i'm using:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [ExecuteInEditMode]
    6. public class AbilityHolder : MonoBehaviour
    7. {
    8.     public GameObject Fishpart;
    9.     public GameObject Fishpart2;
    10.     public List<AttackAbility> Abilities;
    11.     public List<AttackAbility> Abilities2;
    12.     private GameObject fishpartLastFrame;
    13.     private GameObject fishpartLastFrame2;
    14.  
    15.     void Update()
    16.     {
    17.         if (Fishpart != null)
    18.         {
    19.             if (Fishpart != fishpartLastFrame) InitializeAbilities();
    20.         }
    21.         if (Fishpart2 != null)
    22.         {
    23.             if (Fishpart2 != fishpartLastFrame2) InitializeAbilities2();
    24.         }
    25.         fishpartLastFrame = Fishpart;
    26.         fishpartLastFrame2 = Fishpart2;
    27.     }
    28.  
    29.     void InitializeAbilities()
    30.     {
    31.         var i = 0;
    32.         Abilities.Clear();
    33.  
    34.         foreach (Transform t in Fishpart.transform)
    35.         {
    36.             var ability = t.GetComponent<AttackAbility>();
    37.             if (ability)
    38.             {
    39.                 Abilities.Add(ability);
    40.                 i++;
    41.             }
    42.         }
    43.     }
    44.  
    45.     void InitializeAbilities2()
    46.     {
    47.         var i = 0;
    48.         Abilities2.Clear();
    49.  
    50.         foreach (Transform t in Fishpart2.transform)
    51.         {
    52.             var ability = t.GetComponent<AttackAbility>();
    53.             if (ability)
    54.             {
    55.                 Abilities2.Add(ability);
    56.                 i++;
    57.             }
    58.         }
    59.     }
    60.  
    61. }
    62.  


    Any help is greatly appreciated!
     
  2. IllTemperedTunas

    IllTemperedTunas

    Joined:
    Aug 31, 2012
    Posts:
    782
    Just realized i made changes and i'm trying to reference scripts inside the asset and not the correct references disregard this thread :p