Home > Software engineering >  Tranform.Find Isn't Getting the Correct GameObject
Tranform.Find Isn't Getting the Correct GameObject

Time:03-05

I feel like this is a poor question to ask, but I couldn't figure out what the issue was.

I was making a ScoreController and I wanted to make it so that a coin object, when hit, would add 100 to the score GUI. All the coins have this script inside of them:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CoinScoreController : MonoBehaviour
{

    public void OnTriggerEnter2D(Collider2D collider)
    {
        transform.parent.parent.parent.Find("Canvas/ScoreUIController").CollectCoin();
    }
}

I can recognize that this is a very poor way of getting an object but I can't figure out what the issue is. Here is the script that I was trying to reference:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScoreController : MonoBehaviour
{

    public static int score = 0; //Number of coins
    public static GameObject scoreUI; //A gameObject for the CoinUI

    // Update is called once per frame
    void Start()
    {
        scoreUI = GameObject.Find("ScoreUI"); //Automatically sets all coins to have their coinUI GameObject equal the CoinUI UI.
    }

    public void CollectCoin() //Runs when a coin is collected. Makes it disapear and adds one to the counter.
    {
        gameObject.SetActive(false);
        score  = 100;
        scoreUI.GetComponent<Text>().text = "Score: "   score.ToString();
    }

    public void KilledMonster()
    {
        score  = 1000;
    }

}

The ScoreControllerUI is inside Canvas so I don't think I have the wrong path. Can you not transform.Find() from the scene? Here is my scene:

My Scene

As I am sure you can tell, I am new to this site and to Unity, so I apologize for any mistakes in the way I asked this question. Feel free to give me constructive criticism on that as well. I am using unity version 2020.3.24f1 and coding in C#. I don't know if this is relevant but I am coding using visual studio.

Sorry again for any dumb mistakes I made.

CodePudding user response:

Instead of using

transform.parent.parent.parent.Find("Canvas/ScoreUIController").CollectCoin();

use

GameObject.Find("Canvas/ScoreUIController").GetComponent<ScoreController>().CollectCoin();

Suggestion: Define a public ScoreController ScoreUIController in your CoinScoreController (I'm assuming your coin is a prefab) then drag and drop the ScoreUIController into it to assign your script to our defined variable. Now, you would be able to call

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CoinScoreController : MonoBehaviour
{
    public ScoreController ScoreUIController;

    public void OnTriggerEnter2D(Collider2D collider)
    {
        ScoreUIController.CollectCoin();
    }
}

P.S. : Please follow tutorials and read documentation first, as I can see your code is very process heavy.

  • Related