Search Unity

How to set Child As Target in Script

Discussion in 'Scripting' started by Mashimaro7, Jul 1, 2020.

  1. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    How do you assign a child object through script by name? I tried
    Code (CSharp):
    1. attackTrigger = GameObject.Find("EnemyAttackTrigger");
    but it just assigned the first EnemyAttackTrigger, which is another enemy's attack trigger. Is there an easy way to assign the current objects child, without assigning each individually through the inspector?

    Thanks
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    You can use transform.Find instead of GameObject.Find.

    You can also iterate over the direct children of a transform with something like
    Code (CSharp):
    1. foreach (Transform child in parentTransform)
     
    Mashimaro7 likes this.
  3. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Sorry, it doesn't seem to be working, it is a game object, so it says cannot convert transform to game object.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    That just means you need to write ".transform" after your variable. Every GameObject has an attached Transform.

    If you need any further help than that, post the code that is giving you the error.
     
    Mashimaro7 likes this.
  5. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    This time it says cannot assign gameobject.transform, it is read only.
    Here's my script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyAttack : MonoBehaviour
    6. {
    7.     public Animator animator;
    8.     public GameObject attackTrigger;
    9.     public EnemyMover mover;
    10.     public EnemyCreator enemy;
    11.     public bool isAttacking;
    12.     private float speed;
    13.     private float baseSpeed;
    14.     public LayerMask playerLayer;
    15.     public Player player;
    16.     private void Start()
    17.     {
    18.         enemy = this.GetComponent<EnemyCreator>();
    19.         player = FindObjectOfType<Player>();
    20.         attackTrigger.transform = transform.Find("EnemyAttackTrigger");
    21.         attackTrigger.SetActive(false);
    22.         mover = this.GetComponent<EnemyMover>();
    23.         baseSpeed = enemy.speed;
    24.         animator = this.GetComponent<Animator>();
    25.        
    26.     }
    27.     private void Update()
    28.     {
    29.         RaycastHit2D info = Physics2D.Raycast(transform.position, mover.isRight ? Vector2.right : Vector2.left, 1f, playerLayer);
    30.         if (info.collider != null)
    31.         {
    32.             if(!this.GetComponentInParent<EnemyCreator>().isDead && !isAttacking && !player.isDead)
    33.             {
    34.                 StartCoroutine(Attack());
    35.             }
    36.         }
    37.     }
    38.     IEnumerator Attack()
    39.     {
    40.         if (!this.GetComponentInParent<EnemyCreator>().isDead)
    41.         {
    42.             enemy.speed = 0.1f;
    43.             isAttacking = true;
    44.             animator.SetBool("isAttacking", true);
    45.             yield return new WaitForSeconds(0.5f);
    46.             attackTrigger.SetActive(true);
    47.             yield return new WaitForSeconds(0.25f);
    48.             animator.SetBool("isAttacking", false);
    49.             if (attackTrigger != null)
    50.             {
    51.                 attackTrigger.SetActive(false);
    52.             }
    53.                 isAttacking = false;
    54.             enemy.speed = baseSpeed;
    55.         }
    56.     }
    57. }
    58.  
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Always point out which line has the error, too.

    I was imagining that you had a GameObject and needed a Transform. Apparently your situation is the other way around.

    You want to change
    Code (CSharp):
    1. attackTrigger.transform = transform.Find("EnemyAttackTrigger");
    into
    Code (CSharp):
    1. attackTrigger = transform.Find("EnemyAttackTrigger").gameObject;
     
    Mashimaro7 likes this.
  7. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    That worked, thank you very much!