'How can get a string value returning null when using GetComponent on another gameObject script?
public class Character : MonoBehaviour
{
public string characterRace;
public void profile()
{
characterRace = GetComponent<GetData>().SelectedRace as string;
Debug.Log (characterRace);
}
}
`public class GetData : MonoBehaviour {
//Attach this script to a Dropdown GameObject
Dropdown m_Dropdown;
//This is the string that stores the current selection m_Text of the Dropdown
public string m_Message;
//This Text outputs the current selection to the screen
public Text m_Text;
//This is the index value of the Dropdown
public int m_DropdownValue;
public string SelectedRace;
void Start()
{
//Fetch the DropDown component from the GameObject
m_Dropdown = GetComponent<Dropdown>();
}
void Update()
{
//Keep the current index of the Dropdown in a variable
m_DropdownValue = m_Dropdown.value;
//Change the message to say the name of the current Dropdown selection using the value
m_Message = m_Dropdown.options[m_DropdownValue].text;
//Change the onscreen Text to reflect the current Dropdown selection
m_Text.text = m_Message;
if (m_DropdownValue != 0) {
SelectedRace = m_Message;
}
else
{
SelectedRace = "";
}
}
}`
I get an error when i try to get the characterRace value, it returns as null when i call via GetComponent on another GameObject
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
