Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by anosmicAnimator · Dec 10, 2013 at 04:52 PM · arraycharacterrandomarraysstring

Referencing private string for array name

I have a number of arrays which are randomly selected from an array of array names. I have a function that writes the randomly selected array name to a string called newEq. The next function then references this string as the name of an array. But I keep getting the error "Cannot implicitly convert type char' to string'".

 void pickEquation () {
     newEq = Eqs[Random.Range(0, 50)];
     newEquation();
 }


 void newEquation () {
     LeftText.GetComponent<TextVariable>().Ringvariable = newEq[0];
     TopText.GetComponent<TextVariable>().Ringvariable = newEq[1];
     RightText.GetComponent<TextVariable>().Ringvariable = newEq[2];
     BottomText.GetComponent<TextVariable>().Ringvariable = newEq[3];
     Equation.GetComponent<TextVariable>().Ringvariable = newEq[17];
 }
Comment
Add comment · Show 8
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 mujpir · Dec 10, 2013 at 05:00 PM 0
Share

newEq is string and so newEq[i] is a char . you should convert char to string or call ToString() function of newEq[i] .

avatar image anosmicAnimator · Dec 10, 2013 at 05:18 PM 0
Share

Your idea got rid of the error, mujpir, but the process still does not apply the array strings to Ringvariable.

New Code: void pickEquation () { newEq = Eqs[Random.Range(0, 50)]; newEquation(); }

     void newEquation () {
         LeftText.GetComponent<TextVariable>().Ringvariable = (string)newEq[0].ToString();
         TopText.GetComponent<TextVariable>().Ringvariable = (string)newEq[1].ToString();
         RightText.GetComponent<TextVariable>().Ringvariable = (string)newEq[2].ToString();
         BottomText.GetComponent<TextVariable>().Ringvariable = (string)newEq[3].ToString();
         Equation.GetComponent<TextVariable>().Ringvariable = (string)newEq[17].ToString();
     }
avatar image StormSabotage · Dec 10, 2013 at 05:20 PM 0
Share

Do not use casts here! newEq type is 'string' already.

avatar image anosmicAnimator · Dec 10, 2013 at 05:36 PM 0
Share

Removed the casts, and noticed that I didn't have pickEquation initialized. Now it returns 'E' 'Q' '3' in the texts. I imagine its because I converted newEq to a string, but I don't know how to fix it. Code: void pickEquation () { newEq = Eqs[Random.Range(0, 50)]; newEquation(); }

     void newEquation () {
         LeftText.GetComponent<TextVariable>().Ringvariable = newEq[0].ToString();
         TopText.GetComponent<TextVariable>().Ringvariable = newEq[1].ToString();
         RightText.GetComponent<TextVariable>().Ringvariable = newEq[2].ToString();
         BottomText.GetComponent<TextVariable>().Ringvariable = newEq[3].ToString();
         Equation.GetComponent<TextVariable>().Ringvariable = newEq[17].ToString();
     }
 }
avatar image StormSabotage · Dec 10, 2013 at 05:45 PM 0
Share
 LeftText.GetComponent().Ringvariable = Eqs[Random.Range(0, 50)];
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by flaviusxvii · Dec 10, 2013 at 04:57 PM

If newEq is of type 'string' then

 newEq[0]

is grabbing the first char of that string. You can't assign a char to Ringvariable if Ringvariable is a string as well.

Comment
Add comment · Show 1 · 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 anosmicAnimator · Dec 10, 2013 at 07:25 PM 0
Share

THe char isn't what I want. I want newEq to be a referenceable variable, by which I can select the name of the array I want to use.

avatar image
0

Answer by StormSabotage · Dec 10, 2013 at 04:58 PM

Even if your array has 'string' inside, you should use cast.

   ... = (string)newEq[0];

But your 'newEq' is not array, it's 'string'. If you need random names your code will work that way:

 LeftText.GetComponent<TextVariable>().Ringvariable = Eqs[Random.Range(0, 50)];
 TopText.GetComponent<TextVariable>().Ringvariable = Eqs[Random.Range(0, 50)];
 RightText.GetComponent<TextVariable>().Ringvariable = Eqs[Random.Range(0, 50)];
 BottomText.GetComponent<TextVariable>().Ringvariable = Eqs[Random.Range(0, 50)];
 Equation.GetComponent<TextVariable>().Ringvariable = Eqs[Random.Range(0, 50)];
Comment
Add comment · Show 3 · 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 anosmicAnimator · Dec 10, 2013 at 05:12 PM 0
Share
 void newEquation () {
         LeftText.GetComponent<TextVariable>().Ringvariable = (string)newEq[0];
         TopText.GetComponent<TextVariable>().Ringvariable = (string)newEq[1];
         RightText.GetComponent<TextVariable>().Ringvariable = (string)newEq[2];
         BottomText.GetComponent<TextVariable>().Ringvariable = (string)newEq[3];
         Equation.GetComponent<TextVariable>().Ringvariable = (string)newEq[17];


changed the error message to Cannot convert type char' to string'

avatar image StormSabotage · Dec 10, 2013 at 05:14 PM 0
Share

Your 'newEq' is string already, so newEq[0] will retunr 'n' newEq[1] will return 'e' newEq[2] will return 'w' etc...

I edited my answer, check it out

avatar image anosmicAnimator · Dec 10, 2013 at 07:26 PM 0
Share

I need each line of newEquation to reference the same array. this looks like each line chooses a new array.

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to randomly pick a string from an array 3 Answers

changing GUI Button text with a string array 2 Answers

Assigning array element to variable 1 Answer

pick a random int with the value of 1 from an array 2 Answers

How to randomize an array of Strings? 2 Answers


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