- Home /
Cannot convert string to int.
This is my code. I want to make currentScene a string but if I do I get errors from my void update that says "Cannot convert string to int. ". is there a solution to this?
public string currentScene;
public List<SceneElementsList> sceneElements;
public Databases background;
public Databases fruit;
// Update is called once per frame
void Update()
{
background.current_image.sprite = background.stockimages[sceneElements[currentScene].background_image];
fruit.current_image.sprite = fruit.stockimages[sceneElements[currentScene].fruit_image];
}
[System.Serializable]
public class SceneElementsList
{
public string scene_number;
public string background_image;
public string fruit_image;
}
}
Answer by RadonRaph · Nov 16, 2019 at 09:26 AM
Hello @Danielcool20 , if you string is a number ("0", "1", "55") you can convert to int with Int32.Parse(). Read more here: https://www.geeksforgeeks.org/int32-parsestring-method-in-c-sharp-with-examples/
Your problem is that you cannot use String as indexer for an array you must have an int. Why your currentScene variable is a string and not an int ?
Hope that help, Raph
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
cannot make a list with the word hello in it 1 Answer
Update List<> in editor 0 Answers
Is it possible to name a list by a string variable? 2 Answers
How do I make a list of lists? 2 Answers