C# Array Of Class Read Variables
Hello I have a problem. I've made an class array, wich contains a bunch of variables. but the problem is that i don't know how to read the variables from the array.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[System.Serializable]
public class Replies
{
public string ReplyName;
public int nextScenarioID;
}
public class scenario : MonoBehaviour
{
public int ScenarioID;
public Replies[] Replies;
// Update is called once per frame
void Update ()
{
GameObject.Find("ButtonOneChoiceText").GetComponent<Text>().text = "" + Replies[0];
GameObject.Find("ButtonTwoChoiceText1").GetComponent<Text>().text = "" + Replies[1];
GameObject.Find("ButtonTwoChoiceText2").GetComponent<Text>().text = "" + Replies[2];
}
}
so the problem is to set the text components to the corresponding reply. but it has multiple variables, and i don't really get how to select the one i want. so in this case, "ButtonOneChoiceText"s text component should be "Yes", "ButtonTwoChoiceText1"s text component should be "No", and so forth.
Thanks for reading this, Cheers
//Gaffa
Could you add a screenshot of your Inspector, and copy paste the code for your class into your question please. It's quite hard to understand your problem if I don't know what you've attempted/your setup.
Answer by rslnautic · Apr 17, 2016 at 03:42 PM
Use replies[0].replyName and replies[0].nextScenarioID
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[System.Serializable]
public class Replie
{
public string replyName;
public int nextScenarioID;
}
public class scenario : MonoBehaviour
{
public int scenarioID;
public Replie[] replies;
// Update is called once per frame
void Update ()
{
if (replies.Length != 0) {
foreach(Replie repl in replies) {
Debug.Log(repl.replyName+" "+repl.nextScenarioID);
}
}
}
}
Answer by MrDiVolo · Apr 17, 2016 at 04:19 PM
Answer
Consider adding a variable to your Replies class of type Text; you can then use this variable to assign the different Text components to the string values.
Then in your Scenario class, in Start
you could iterate through the array, and call a method to set up the text components;
public class Reply
{
[SerializeField]
private string m_replayName;
[SerializeField]
private int m_nextScenario;
[SerializeField]
private Text m_text;
public void Setup()
{
m_text.text = m_replayName;
}
}
public class Scenario
{
[SerializeField]
private Reply[] m_replies;
public void Start()
{
foreach(Reply r in replies)
r.Setup();
}
}
Advice/Tips
Use the SerializeField attribute to expose (view) private variables in the Inspector, and where needed create a method to get/set the value of this private variable. (private variables are generally best practice, if you want to know more watch this tutorial on encapsulation [video in Java, but it's about the concept of Encapsulation])
When naming classes, generally people use the singular version of a word (general coding convention).
Hope this helped in addition to the previous answer.
Wow, thanks for the help! But why do you start your variables with m_ ?
This is to distinguish the member variables (sometimes called instance variables; private variables belonging to the class) from local variables (for which I give them no prefix). As well as this, I also use p_
for parameter variables, to again distinguish those from any local or member variables.
m_
is fairly stereotypical of C# program$$anonymous$$g (or so I'm told), but for parameters many people just use _
as the prefix. (However, because I've learnt C++ as well, starting variables with underscores used to cause problems, so I swapped to p_
).
For future reference (you might know this); in C#, the convention is to use what is called camelCase for your variables, and PascalCase for your methods. This is not the same for all languages, and you will have to look up the conventions of the specific language you're using.
Glad my answer helped and hope this did too.