- Home /
FindGameObjectWithTag wont find in Start() or Awake() but will in Update()
Hello, I am having an issue with FindGameObjectWithTag, I've attached this line of code, if I move it into Update() it works properly but if it is in the Start()or Awake() I get a null reference. I assume this is because the Start()and Awake() functions call it to early but I have no idea how to fix this issue and don't want to leave it in update because it is expensive. Any insight would be greatly appreciated.
GameTrack = GameObject.FindGameObjectWithTag("Tracker");
Is the object created in the Start of another script? It would be surprising that this object is already placed in the scene and not found in Start or Awake.
Thank you for your reply, the object is already in the scene, it will be a persistent game object through all of my scenes, I used DontDestroyOnLoad() in the script on that object so it will always be there.
This is unusual behaviour since objects are created before any of the Start methods. Is the object active?
I'm not sure what you mean by active, its an empty game object with a script attached to it, the only things it has in the inspector are a transform and the script. (I'd attach a picture but its not letting me upload one)
Do you have the tick on in the box left to the name in the inspector? If the object is not active Find methods won't find it.
Just narrowing the possibilities.
Answer by ThePunisher · Apr 30, 2014 at 08:56 PM
If the object is always readily available in the scene why not just link it through a public inspector property? You would normally use tags to retrieve objects if either object was created dynamically.
Assuming you don't change/set the tag of the "Tracker" object as part of some initialization and it's already set to that in the scene (before hitting play) you should be able to find it in the awake/start of any script. This tells me you're either destroying the object and recreating it or updating its tag to be "Tracker" at some point during awake/start. Can you show us the code of the script attached to the "Tracker" object and the code looking for the "Tracker" object.
//this is the code attached to the object "Tracker"
using UnityEngine; using System.Collections;
public class ResourceController : $$anonymous$$onoBehaviour {
public int PlayerGold;
public int PlayerRum;
public int PlayerCrystal;
private string levelName;
// Use this for initialization
void Start () {
DontDestroyOnLoad(this);
}
// Update is called once per frame
void Update () {
}
public void pickUpGold(int ChangeGold){
PlayerGold += ChangeGold;
if (PlayerGold > 9999){
PlayerGold = 9999;
}
}
public void pickUpRum(int ChangeRum){
PlayerRum += ChangeRum;
if (PlayerRum > 9999){
PlayerRum = 9999;
}
}
public void pickUpCrystal(int ChangeCrystal){
PlayerCrystal += ChangeCrystal;
if (PlayerCrystal > 9999){
PlayerCrystal = 9999;
}
}
public int getGold(){
return PlayerGold;
}
public int getRum(){
return PlayerRum;
}
public int getCrystal(){
return PlayerCrystal;
}
}
//This is the code that is trying to locate the tracker
using UnityEngine;
using System.Collections;
public class HUDNumbers: $$anonymous$$onoBehaviour {
public int position;
private int number;
private Vector2 numberloc;
GameObject GameTrack;
// Use this for initialization
void Start() {
UpdateGold();
UpdateRum();
UpdateCrystal();
GameTrack = GameObject.FindGameObjectWithTag("Tracker");
}
// Update is called once per frame
void Update () {
}
public void UpdateGold(){
ResourceController Go = GameTrack.GetComponent<ResourceController>();
number = Go.getGold();
UpdateNumber();
}
public void UpdateRum(){
ResourceController Ru = GameTrack.GetComponent<ResourceController>();
number = Ru.getRum();
UpdateNumber();
}
public void UpdateCrystal(){
ResourceController Cr = GameTrack.GetComponent<ResourceController>();
number = Cr.getCrystal();
UpdateNumber();
}
void UpdateNumber(){
//Debug.Log("number =" + number);
if (position == 1){
//Debug.Log("1's =" + (number % 10) / 1);
number = (number % 10) / 1;
}
if (position == 2){
if (number >= 9){
//Debug.Log("10's =" + (number % 100) / 10);
number = (number % 100) / 10;
} else {
number = 10;
}
}
if (position == 3){
if (number >= 99){
//Debug.Log("100's =" + (number % 1000) / 100);
number = (number % 1000) / 100;
} else{
number = 10;
}
}
if (position == 4){
if (number > 999){
//Debug.Log("1000's =" + number / 1000);
number = number / 1000;
if (number == 10){
number = 9;
}
} else{
number = 10;//9999
}
}
switch (number){
case 1:
numberloc = new Vector2 (1.0f,.75f);
break;
case 2:
numberloc = new Vector2 (.25f,.75f);
break;
case 3:
numberloc = new Vector2 (.5f,.75f);
break;
case 4:
numberloc = new Vector2 (.75f,.75f);
break;
case 5:
numberloc = new Vector2 (1.0f,.5f);
break;
case 6:
numberloc = new Vector2 (.25f,.5f);
break;
case 7:
numberloc = new Vector2 (.5f,.5f);
break;
case 8:
numberloc = new Vector2 (.75f,.5f);
break;
case 9:
numberloc = new Vector2 (1.0f,.25f);
break;
case 0:
numberloc = new Vector2 (.25f,.25f);
break;
case 10:
numberloc = new Vector2 (0.0f,0.0f);
break;
}
renderer.material.SetTextureOffset ("_$$anonymous$$ainTex", numberloc);
}
}
I tried linking these together in the inspector but since this is an object that exists between all of the scenes it wasn't working. I assumed if I used code to have the objects locate the proper pieces they would work even when changing scenes. Is there a simpler/more efficient way to achieve objects communicating even when they aren't originally located in the same scene?
Oh, so they aren't in the same scene? Which object gets loaded first? The tracker or the HUDNumbers?
Also, can you please verify that the actual tag is "Tracker" and doesn't just have a space in there by accident. If the object being tagged as "Tracker never changes or there aren't multiple trackers then just use this to retrieve it:
GameTrack = GameObject.FindObjectOfType(typeof(ResourseController));