Pretty please help me with my monobehaviours

Hello, I am very new to unity and Im trying to load and read a text file aswell as some other stuff within my script. The code for this script is below.

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

public class ExampleClass : MonoBehaviour
{
    private GameObject Target;
  
    void Start()
    {

     Target = GameObject.Find("Itemtext");
     Debug.Log(Target.name);
    }
}

public class TextFileLoader : MonoBehaviour
{
    void Start()
    {
        // Move the resource loading code to Start() method
        LoadTextFile();
        Debug.Log("Perknames Loaded");
    }

    void LoadTextFile()
    {
        TextAsset textFile = Resources.Load<TextAsset>("Perknames");
        if (textFile != null)
        {
            string fileContent = textFile.text;
            Debug.Log(fileContent);
            string[] stringArray = fileContent.Split(',');
            Debug.Log(stringArray[0]);
            List<string> stringList = new List<string>(stringArray);
            foreach (string str in stringList)
            {
                Debug.Log(str);
            }
        }
        else
        {
            Debug.LogError("Failed to load text file.");
        }
    }
}

Can someone please help me understand why my second Monobehaviour isnt getting called? I am having a hard time understanding this. And before you say it, yes this script is attatched to a gameobject. The first Monobehaviour is running fine.

Thank you for your time.

Don’t do two monobehaviours in the same file.

I believe Unity only bothers with the Start and Update and other built in functions of only the one that has the same name as the filename.

2 Likes

Yes, this ^ ^ ^

ALSO:

Remember the first rule of GameObject.Find():

Do not use GameObject.Find();

More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

More information: https://discussions.unity.com/t/899843/12

In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.
If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way™ success of accessing things in your game.

ALSO:

Just make a public TextAsset textFile; and drag the text file in.

Set yourself up for success, not disaster.

“Stop playing ‘Where’s GameWaldo’ and drag it in already!”

1 Like

How do I “Drag the references in?”

What am i dragging and to where? dragging from my unity scene to my IDE?

Sorry if there is a very newbie question.

From the Hierarchy to the Inspector. Your code has to be written with this in mind though. The field has to either be public or be marked for serialization.

public GameObject target;
[SerializeField] private GameObject target;

This is a core mechanism in Unity. Definitely get familiar with it immediately.

Imphenzia / imphenzia - super-basic Unity tutorial: