- Home /
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!
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.
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.
}
}
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.
Your answer
Follow this Question
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
GUI password check question 2 Answers
Add a value to a string 1 Answer