Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 matt smitih · Feb 06, 2011 at 11:03 PM · c#stringpackageformat

what package?namespace? do I need to make use of String.Format() for C# programs?

I've found a bunch of C# reference materials on how to format strings using String.Format()

e.g.

 timeText = String.Format ("{0:00}:{0:00}:{1:00}:{0:00}",displayDays,displayHours, displayMinutes, displaySeconds);

But ... when I write a C# script in Unity I attempting to make use of String.Format() I get the error message:

The name "String" does not exist in the current content

So I assume I need to add a "using .." statement at the top of of the script file .. but which one?

Using String; doesn't work

any help much appreciated ...

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Feb 06, 2011 at 11:19 PM

"String" = Javascript, "string" = C#.

Comment
Add comment · Show 4 · 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
avatar image yoyo · Feb 06, 2011 at 11:33 PM 1
Share

"string" in C# is just shorthand for "System.String" (just as "int" is shorthand for "System.Int32", etc.)

avatar image Eric5h5 · Feb 06, 2011 at 11:49 PM 0
Share

Indeed, but that doesn't change anything, and isn't particularly relevant here.

avatar image yoyo · Feb 07, 2011 at 12:04 AM 0
Share

OP was asking what namespace to use, answer is System. Understanding aliases for basic types is relevant to understanding that every single type in the language is an object type in a namespace.

avatar image Eric5h5 · Feb 07, 2011 at 12:36 AM 0
Share

Yes, but you don't need it; you can just use string.Format. (Or String.Format in JS.)

avatar image
0

Answer by matt smitih · Feb 06, 2011 at 11:12 PM

okay - slightly odd - I found a different article that used "string.Format()" (i.e. lower case "s")

and this solves the problem :-)

since I assume (perhaps wrongly) that I'm not the only person who might find this odd, I'll leave my question and answer posted, so it might help someone else save 20 minutes trying to get String.Format() rather than string.Format() working in C# scripts

happy coding ... matt ..

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
avatar image
0

Answer by yoyo · Feb 06, 2011 at 11:32 PM

You need ...

using System;
Comment
Add comment · Show 2 · 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
avatar image Eric5h5 · Feb 06, 2011 at 11:45 PM 0
Share

This can cause namespace conflicts. For example Random.Range; Unity doesn't know if that's supposed to be System.Random or UnityEngine.Random, so then you have to explicitly specify, which is a bit of a pain.

avatar image yoyo · Feb 07, 2011 at 12:06 AM 0
Share

I've got "using System;" at the top of dozens and dozens of scripts and in case of conflict I use "using Random = UnityEngine.Random;". I don't have many conflicts, but I guess it depends how random your game is ;)

avatar image
0

Answer by matt smitih · Mar 09, 2011 at 08:35 PM

here's a more explicit version of my final working code (simplified for exposition):

void OnGUI() { float displayDays = 13; float displayHours = 9; float displayMinutes = 32; float displaySeconds = 55; string timeText = System.String.Format ("{0:00}:{0:00}:{1:00}:{0:00}",displayDays,displayHours, displayMinutes, displaySeconds); GUILayout.Label("time = " + timeText );

 }

This has been very illuminating - it is very useful to learn about these "aliases" of classes to act as primitive types (personally I've never been happy with primitive types in any OO language, they spoil the clarity and simplicity of taking an objects-only view - I've always had a softspot for Smalltalk ...)

I found the following link useful, which summarises a list of aliases and the actual classes they are aliases for:

http://en.csharp-online.net/CSharp_FAQ:_What_is_a_type_alias

while UnityAnswers keeps asking me if I want to answer my own question, I keep saying yes ... since I think this discussion is something useful for people like me who can program (I've been programming for over 30 years - they keep on creating new programming concepts like design patterns and object-orientation, and lots of new languages like Python and ActionScript) but are new to C# and unity ...

now to get back to that pacman tile-based world game sample I'm getting ready for my lecture on Friday to some year 2 BSc computing students...

.. matt ..

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Distribute terrain in zones 3 Answers

String format to show float as time 3 Answers

Multiple Cars not working 1 Answer

System.DateTime truncate Milliseconds 1 Answer

How to convert timer to minutes, seconds and cents 3 Answers


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