- Home /
How do I get this string out of this array?
Hi, I'm using the array class becuase I need to have an array that changes size dynamically so I'm trying to access a variable stored in it that should be a String and then assign that String to a TextMesh object's text component. Here is the code I have so far.
print(array1[counter]);
instanceMesh.text = array1[counter];
But I get this error: InvalidCastException: Cannot cast from source type to destination type. Any ideas? The print works fine, but it gets stuck on the next line. If I left something out that would be helpful in solving this just post a comment and I'll reply as quickly as I can. Thanks.
You should just a
List.<String>
To store a variable size list of strings, don't use Array/ArrayList (see this.
array1[counter].ToString()
Will convert it.
Answer by Mr.Z · Jan 04, 2013 at 02:04 PM
InvalidCastException: error, but I now have a "NullReferenceException: Object reference not set to an instance of an object"
array1[counter] could be null at some point of your loop (I assume thats what u are using). Or if array1 is a class (I assume it's not since the name doesn't suggest that), you could be trying to set the value for the string instanceMesh.Text to be a class, which will never work. Or instanceMesh.text might not have been initialized. Also, you might wanna check this answer out: http://answers.unity3d.com/questions/27678/nullreferenceexception-object-reference-not-set-to.html
It was the null at a lot of places. I replaced the null values with 0. Thanks.
Answer by Aldo · Jan 03, 2013 at 07:03 PM
not the best way but do this
instanceMesh.text = ""+array1[counter];
Thanks that got rid of the InvalidCastException: error, but I now have a "NullReferenceException: Object reference not set to an instance of an object" error. Any ideas what I could do? or what I should look for?
Your answer
Follow this Question
Related Questions
Find the length of a string in Javascript 1 Answer
Can't use remove with string[] ? 1 Answer
Iterating through multidimensional arrays 2 Answers
Comparing a string to an array extraction? 1 Answer
Get JSON array object string value 2 Answers