How to multiply by a GUIText.Text

I need to multiply an int by a string…

	public int scoreAdded;

	public GUIText Multiplier;

	void OnCollisionEnter(){

		int multiplier = Multiplier.text; // This is the issue

		Score.score += (scoreAdded * multiplier);
		Score.progressMulti++;
		for(int i = 0; i < amount; i++)

I know why it doesn’t work but I can’t seem to find a way to do this.

Not sure if that’s the best design choice (ie why is the Multiplier a GUIText and can’t be anything else?), but a quick Google search for C# string to int…

An example tested right now in Unity…

        string testS = "10";
        int testI = System.Convert.ToInt32 (testS);
        print (testI);

Edit: Sorry if I didn’t directly answer your question, but what I meant by the first sentence is I’m not sure why you would necessarily need to multiply the GUIText directly/as-is/etc. Sort of burnt out tonight after trying to figure something out, so I couldn’t wrap my mind around your code properly. However, converting the text to int should solve the issue. HOWEVER, I haven’t tested yet if GUIText.Text is specifically a string type, and I’ll await a reply to see if I should try to rethink these short solutions :slight_smile:

To reinforce what MDragon said above…

While you definitely can convert a string to an integer (provided the string value can be converted), that’s probably not the best choice. Presumably, your GUIText item is just a visual representation of some internal variable’s value. By your description, that variable is probably already an integer and is just being displayed in a GUIText item. So, you should be using the actual integer variable for your multiplication, not the string representation of that variable held in the GUIText item.

Jeff

Thank you… I took your advice on the bad design and decided to call the int that I was casting into the GUI. I just needed to move it into the Start Function instead of the Collision. That was my initial problem.