Search Unity

.sendmessage to child object?

Discussion in 'Scripting' started by TyrantKoala, Nov 11, 2018.

  1. TyrantKoala

    TyrantKoala

    Joined:
    Jul 24, 2017
    Posts:
    9
    So I have a characterList and on this list I have a bunch of characters. I created a playerpref which records the name of the current gameobject and it appears to work. But when I try to find the object based on the playerpref, the thing does not find it. Heres code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class finishImpact : MonoBehaviour {
    6.  
    7.     private float doOnce = 0;
    8.     public string characName;
    9.  
    10.     public void Start()
    11.     {
    12.         characName = PlayerPrefs.GetString("CharacterName");
    13.         Debug.Log(characName);
    14.     }
    15.  
    16.     private void OnTriggerEnter(Collider other)
    17.     {
    18.        
    19.         if (doOnce == 0)
    20.         {
    21.  
    22.             Debug.Log(characName +"Is the name");
    23.             GameObject.Find("CharacterList").SendMessage("Finished");
    24.            
    25.             GameObject.Find(characName).SendMessage("raceOver");
    26.             Debug.Log("Player Has Finished!");
    27.             doOnce = 1;
    28.         }
    29.        
    30.     }
    31. }
    32.  

    In the console i always get an error saying it could not find the GameObject(string).
     
  2. Reeii

    Reeii

    Joined:
    Feb 5, 2016
    Posts:
    91
    Do you mean you have a "characterList" game object in your scene and the characters are its children? If yes, then do:
    Code (CSharp):
    1. GameObject.Find("CharacterList/" + characName);
    You have to state each object in the parent hierarchy divided by a slash.
     
    TyrantKoala likes this.
  3. TyrantKoala

    TyrantKoala

    Joined:
    Jul 24, 2017
    Posts:
    9
    that did it thanks!