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 convert object’ expression to type `char*’

Any help would be very much appreciate ! Thanks a lot !

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. (String Class (System) | Microsoft Learn)

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

Hope this helped.