Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Gaffa80 · Apr 17, 2016 at 12:20 PM · c#arrayclassesindex

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.

alt text

 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

imguuuuura.png (26.4 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrDiVolo · Apr 17, 2016 at 02:41 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
              }
          }
      }
  }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

  1. 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])

  2. When naming classes, generally people use the singular version of a word (general coding convention).

Hope this helped in addition to the previous answer.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gaffa80 · Apr 17, 2016 at 04:23 PM 0
Share

Wow, thanks for the help! But why do you start your variables with m_ ?

avatar image MrDiVolo Gaffa80 · Apr 17, 2016 at 05:03 PM 0
Share

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.

$$anonymous$$icrosoft C# conventions Other C# practices

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

(SOLVED) Accessing (string) arrays by enumerations from another class 0 Answers

Unknown Argument Out of Range Index Error On Card Game 1 Answer

[help] Array index is out of range 0 Answers

null reference on instantiated prefab array at instantiation 1 Answer

Calling Function from another script not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges