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 michse · Apr 29, 2014 at 03:51 PM · stringvaluetype

how to assign the value of a string as variable

hi there, i´m having a problem and i m not able to explain it, because if i had the words for it, i would know for what to google.

I think it is easier to understand in code:

 int stepper = 1;
 string myNewString = "tv" + stepper +".mainTexture";
 tvtvtv.renderer.material.mainTexture = "myNewString";
 

So what i m trying to acclomplise is to assign

 tvtvtv.renderer.material.mainTexture = tv1.mainTexture;

in a dynamic way. But i dont know how to assign the value of the string in stead of the string variable itselfe

So how do i do that? What do i need to google for, or what does the trick. THX for every helpful Answer!

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Dameon_ · Apr 29, 2014 at 07:23 PM

What you're asking isn't impossible, but it is not generally a good or necessary approach. It's something a lot of people attempt to do at some point, but there's lots of flaws in doing it that way. If you find yourself wanting to dynamically name variables, think about how else the same thing can be accomplished with other methodology.

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 kromenak · Apr 29, 2014 at 05:32 PM

You have a variable type problem here - mainTexture, as outlined in the documentation, is type "Texture". You are trying to assign type "string" to that variable, which causes a compiler error. You're trying to store the value "tv1.mainTexture", a string variable, as a Texture variable, and the compiler has no idea what you're trying to do.

This is really a fundamental programming/object-oriented concepts issue - a Texture is a Texture, not a string. In your mind, it makes sense that you're trying to assign this texture using the name, but the compiler only sees a string of characters, not the texture object/data itself.

To assign a texture in Unity, you'd need to do something like this (C# code):

 public class MyClass : Monobehaviour
 {
     // This texture shows up in the inspector, so you can assign a value to it.
     public Texture myTexture;
 
     // This shows up as an array of textures, so you can assign multiples.
     public Texture[] myTextureArray;
 
     private void Awake()
     {
         // Assign texture.
         renderer.material.mainTexture = myTexture; // works because mainTexture & myTexture are of type Texture.
 
         gameObject.name = "tv1.mainTexture"; // works because name is of type string.
     }
 
 }





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 michse · Apr 29, 2014 at 07:24 PM

hi there, thx for looking into it.

i know about the type problem. sry, that i dont outlined that i m beyond that point (i only pasted the (edited) part of the script that seemed relevant to me.

i was able to create a working script, but it isnt very dynamic:

 using UnityEngine;
 using System.Collections;
 
 public class tvtest1 : MonoBehaviour {
     public float mytime;
     public int stepper;
     public GameObject tvtvtv;
     public Material tv1;
     public Material tv2;
     public Material tv3;
     public Material tv4;
     public Material tv5;
     public Material tv6;
     public Material tv7;
     public Material tv8;
     public Material tv9;
     public Material tv10;
     public Material tv11;
     public Material tv12;
     public Material tv13;
     public Material tv14;
     public Material tv15;
     public Material tv16;
     public Material tv17;
     public Material tv18;
     public Material tv19;
     public Material tv20;
     public Material tv21;
     public Material tv22;
     public Material tv23;
     public Material tv24;
     public Material tv25;
     
     Color color;
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         mytime = (Time.time% 1)*25;
         stepper = (int)mytime;
 
 
 
         if (stepper == 0) {
             tvtvtv.renderer.material.mainTexture = tv1.mainTexture;
                 }
         if (stepper == 1) {
             tvtvtv.renderer.material.mainTexture = tv2.mainTexture;
         }
         if (stepper == 2) {
             tvtvtv.renderer.material.mainTexture = tv3.mainTexture;
         }
         if (stepper == 3) {
             tvtvtv.renderer.material.mainTexture = tv4.mainTexture;
         }
         if (stepper == 4) {
             tvtvtv.renderer.material.mainTexture = tv5.mainTexture;
         }
         if (stepper == 5) {
             tvtvtv.renderer.material.mainTexture = tv6.mainTexture;
         }
         if (stepper == 6) {
             tvtvtv.renderer.material.mainTexture = tv7.mainTexture;
         }
         if (stepper == 7) {
             tvtvtv.renderer.material.mainTexture = tv8.mainTexture;
         }
         if (stepper == 8) {
             tvtvtv.renderer.material.mainTexture = tv9.mainTexture;
         }
         if (stepper == 9) {
             tvtvtv.renderer.material.mainTexture = tv10.mainTexture;
         }
         if (stepper == 10) {
             tvtvtv.renderer.material.mainTexture = tv11.mainTexture;
         }
         if (stepper == 11) {
             tvtvtv.renderer.material.mainTexture = tv12.mainTexture;
         }
         if (stepper == 12) {
             tvtvtv.renderer.material.mainTexture = tv13.mainTexture;
         }
         if (stepper == 13) {
             tvtvtv.renderer.material.mainTexture = tv14.mainTexture;
         }
         if (stepper == 14) {
             tvtvtv.renderer.material.mainTexture = tv15.mainTexture;
         }
         if (stepper == 15) {
             tvtvtv.renderer.material.mainTexture = tv16.mainTexture;
         }
         if (stepper == 16) {
             tvtvtv.renderer.material.mainTexture = tv17.mainTexture;
         }
         if (stepper == 17) {
             tvtvtv.renderer.material.mainTexture = tv18.mainTexture;
         }
         if (stepper == 18) {
             tvtvtv.renderer.material.mainTexture = tv19.mainTexture;
         }
         if (stepper == 19) {
             tvtvtv.renderer.material.mainTexture = tv20.mainTexture;
         }
         if (stepper == 20) {
             tvtvtv.renderer.material.mainTexture = tv21.mainTexture;
         }
         if (stepper == 21) {
             tvtvtv.renderer.material.mainTexture = tv22.mainTexture;
         }
         if (stepper == 22) {
             tvtvtv.renderer.material.mainTexture = tv23.mainTexture;
         }
         if (stepper == 23) {
             tvtvtv.renderer.material.mainTexture = tv24.mainTexture;
         }
         if (stepper == 24) {
             tvtvtv.renderer.material.mainTexture = tv25.mainTexture;
         }
         if (stepper == 25) {
             tvtvtv.renderer.material.mainTexture = tv25.mainTexture;
         }
 
 
 
 
 
     }
 }

so i m able to assign the the texture. i want to do it more dynamic, so i can do it without the if-s. my question was how to submit the value of a string, not the string itselfe.

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

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

22 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 avatar image avatar image

Related Questions

How Can I Change GetType() And GetField() And GetValue() ? 2 Answers

What is typing a variable? Is it assigning the value? 3 Answers

My HighScore Stuck on a ceratin value and wont update.(in game HighScore works, only problem when game is restarted the value of highscore is fixed) 0 Answers

GUI password check question 2 Answers

Add a value to a string 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