• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by noniol · Jan 22, 2013 at 03:44 AM · c#errortutorial

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 !

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vonni · Jan 23, 2013 at 05:58 PM 0
Share

which line(s) gives the error(s)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Particle circle HELP 1 Answer

CS0118 Error 1 Answer

Adding value to GameObject[] 1 Answer

How do I fix UnityEngine.Transform' does not contain a definition for `GameObject'? 2 Answers

Semicolons are seen as an unexpected symbol 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges