- Home /
Following the wiki.unity scripting tutorial...
Hello, so i'm just starting scripting in Unity (C#) and i'm hitting a road block, not being able to figure out what i am doing wrong. It's probably a very easy solution, but i really can't see it :s
here's the original script given in the tutorial:
using UnityEngine;
using System.Collections;
public class Operation : MonoBehaviour
{
// This function has no parameters and returns nothing
void OnGUI()
{
float playerStrength = 10.5f;
int playerLives = 3;
string playerName = "Player";
bool isPlayerAlive = true;
// Player got a 1-Up
playerLives++;
GUILayout.Label("Lives: " + playerLives.ToString());
// Player got a special bonus and is now Super Player
playerName = "Super " + playerName;
GUILayout.Label(playerName);
// Player got hit by a trash can
playerStrength--;
GUILayout.Label("Str: " + playerStrength.ToString());
// Is the player still alive?
isPlayerAlive = playerStrength > 0.0f;
playerName += isPlayerAlive ? " Alive" : " Dead";
GUILayout.Label(playerName);
// Player gets hit by 2 meteors
// Since we're using an int and float in the operation, the compiler
// will automatically cast the 2 to a float as an operation requires
// 2 of the same type to work
playerStrength -= 2 * 5000f;
GUILayout.Label("Str: " + playerStrength.ToString());
isPlayerAlive = playerStrength > 0.0f;
playerLives = isPlayerAlive ? playerLives : playerLives-1;
GUILayout.Label("Lives: " + playerLives.ToString());
playerName += isPlayerAlive ? " Alive" : " Dead";
GUILayout.Label(playerName);
}
}
and here's what i'm trying to do, which is making the playerName being a new string, as opposed to carrying its first state resulting in ("Super Player is Alive Dead")
using UnityEngine;
using System.Collections;
public class Operation : MonoBehaviour
{
// This function has no parameters and returns nothing
void OnGUI()
{
float playerStrength = 10.5f;
int playerLives = 3;
string playerName = "Player";
bool isPlayerAlive = true;
// Player got a 1-Up
playerLives++;
GUILayout.Label("Lives: " + playerLives.ToString());
// Player got a special bonus and is now Super Player
playerName = "Super " + playerName;
GUILayout.Label(playerName);
// Player got hit by a trash can
playerStrength--;
GUILayout.Label("Str: " + playerStrength.ToString());
// Is the player still alive?
isPlayerAlive = playerStrength > 0.0f;
playerName += isPlayerAlive ? " Alive" : " Dead";
GUILayout.Label(playerName);
// Player gets hit by 2 meteors
// Since we're using an int and float in the operation, the compiler
// will automatically cast the 2 to a float as an operation requires
// 2 of the same type to work
playerStrength -= 2 * 5000f;
GUILayout.Label("Str: " + playerStrength.ToString());
isPlayerAlive = playerStrength > 0.0f;
playerLives = isPlayerAlive ? playerLives : playerLives-1;
GUILayout.Label("Lives: " + playerLives.ToString());
playerName = new string ("Player");
playerName += isPlayerAlive ? " Alive" : " Dead";
GUILayout.Label(playerName);
}
}
The errors i am getting are
"The best overloaded method match for
string.String(char*)' has some invalid arguments" - "Argument
#1 cannot convertobject' expression to type
char*'
Any help would be very much appreciate ! Thanks a lot !
Answer by ATMEthan · Jan 23, 2013 at 05:51 PM
To overwrite a string you just do playerName = "player";
To reassign a value to string you shouldn't call a string constructor. That is used for creating a string from a char[] or Int32. (http://msdn.microsoft.com/en-us/library/8s83eyw9.aspx)
If you notice in your error message the compiler says: Cannot convert object(meaning "Player") to type char(meaning any legal char character like 'a'). So what you are trying to do in the constructor is convert string to a char which isn't possible. Here's an example of when you would want to use the string constructor.
return new string(reversedString); //this goes hand and hand with the link below
http://stackoverflow.com/questions/3562078/why-am-i-getting-system-char-printed-out-in-this-case
Hope this helped.
Your answer
Follow this Question
Related Questions
Official Unity Space Shooter Tutorial 1 Answer
I can do this in JS or not ??? 2 Answers
Unity Internal Compiler Error, not there in Editor 0 Answers
How can i call a GUI function? 3 Answers