Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

The Idea: Pressing a button in the canvas will allow the player to select a race. A script is attached to an object which is accessed though the OnClick() function of the button.

The script is supposed to get which race is selected based on which button is pressed and fetch a race description in a Text Object named "DescriptionText".

However a description is not displayed, and I get this error: "NullReferenceException: Object reference not set to an instance of an object GOnClick.GetRaceDesc () (at Assets/Scripts/GUI_Scripts/GOnClick.cs:24)".

I can't see what I am doing wrong, so here is my code:

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

public class GOnClick : MonoBehaviour {

public string race, description;
void Start()
{
    race = "";
    description = "";
}

void Update()
{
    GetRaceDesc();
}


public void GetRaceDesc()
{
    //Button butnText = GameObject.FindObjectOfType<Button>();
    //race = butnText.GetComponentInChildren<Text>().text;
    *(line 24)* race = gameObject.GetComponentInChildren<Text>().text;
    GameObject descRect = GameObject.Find("DescriptionText");


    if(race == "Dark Elf")
    {
        BaseRace raceSelection = new DarkElfRace();
        descRect.GetComponent<Text>().text = raceSelection.CharacterRaceDesc;
    }
    else if (race == "High Elf")
    {
        BaseRace raceSelection = new HighElfRace();
        descRect.GetComponent<Text>().text = raceSelection.CharacterRaceDesc;
    }
    else if (race == "Wood Elf")
    {
        BaseRace raceSelection = new WoodElfRace();
        descRect.GetComponent<Text>().text = raceSelection.CharacterRaceDesc;
    }
    else if (race == "Human")
    {
        BaseRace raceSelection = new HumanRace();
        descRect.GetComponent<Text>().text = raceSelection.CharacterRaceDesc;
    }
    else if (race == "Dwarf")
    {
        BaseRace raceSelection = new DwarfRace();
        descRect.GetComponent<Text>().text = raceSelection.CharacterRaceDesc;
    }
    else if (race == "Orc")
    {
        BaseRace raceSelection = new OrcRace();
        descRect.GetComponent<Text>().text = raceSelection.CharacterRaceDesc;
    }
    else if (race == "Goblin")
    {
        BaseRace raceSelection = new GoblinRace();
        descRect.GetComponent<Text>().text = raceSelection.CharacterRaceDesc;
    }
    else if (race == "Tiefling")
    {
        BaseRace raceSelection = new TieflingRace();
        descRect.GetComponent<Text>().text = raceSelection.CharacterRaceDesc;
    }
    else if (race == "Half Dragon")
    {
        BaseRace raceSelection = new HalfDragonRace();
        descRect.GetComponent<Text>().text = raceSelection.CharacterRaceDesc;
    }
    else if (race == "Werewolf")
    {
        BaseRace raceSelection = new WerewolfRace();
        descRect.GetComponent<Text>().text = raceSelection.CharacterRaceDesc;
    }

    }

}

Thanks in advance for any help.

share|improve this question
    
It's not very clear what is the issue you're facing, could you edit your question? –  Alexandre Vaillancourt Jul 6 at 20:25
    
@AlexandreVaillancourt First of, thank you for being polite. Highly appreciated brother! Is my question more clear now? –  Karaflakos Jul 6 at 20:38
    
It is! Presumably, your gameObject.GetComponentInChildren<Text>() is probably null. Are you certain that the gameObject component hierarchy has a Text component? –  Alexandre Vaillancourt Jul 6 at 20:54
    
@AlexandreVaillacourt Turns out it is not accessing a button's text because it's trying to find a Text component of the object it's attached to which is an empty object with no child components. How would I access the button through the script? –  Karaflakos 2 days ago
    
Sorry, I don't know about Unity enough to help you further. –  Alexandre Vaillancourt 2 days ago

1 Answer 1

So your new question is How you would access the button through the script? I'd create the gameobject string and have it public so you can relate to a specific object in your scene. Then do an "OnGUI" you would name the var that you set up when you create the gameobject string. Hope that helped! :D

share|improve this answer
    
Could you add an example in code tag? it would be easier for me to read and understand. –  Karaflakos 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.