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 ineteye · Oct 14, 2012 at 09:03 AM · c#

Need help converting js to C# -- GUIStyle

Hi! Need help to convert to C#

JS code:

 GUIStyle style;
 
 void OnGUI ()
 {
     style.fontSize=24;
     style.normal.textColor=Color.red;
     style.alignment=TextAnchor.MiddleCenter;
     GUI.Label ( new Rect(Screen.width/2-300, Screen.height/2-20, 600, 40), "TEST",style);
 }


it was Js code... i converted it but have error in this line:

style.fontSize=24; //NullReferenceException: Object reference not set to an instance of an object winLostNew.OnGUI ()

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Seth-Bergman · Oct 14, 2012 at 09:08 AM

you mean you want to convert this C# to javascript.. ok:

 var style : GUIStyle;
 
 function OnGUI ()
 {
     style.fontSize=24;
     style.normal.textColor=Color.red;
     style.alignment=TextAnchor.MiddleCenter;
     GUI.Label ( Rect(Screen.width/2-300, Screen.height/2-20, 600, 40), "TEST",style)
 }

(javascript)

your code is already in c#.. of course you would need to encapsulate this in a class (which inherits from monobehavior) with C#, as this is not handled implicitly as with javascript-based unityscript.. but this is created automatically when you create a new c# script from within unity..

EDIT :

my mistake, I should have paid closer attention to the question. the data member "style" is probably private by default. If you declare it public:

public GUIStyle style;

then you could access it directly via the inspector to set this all up at any rate, and I suspect this may somehow be causing the issue at hand, though I have no idea why..

http://docs.unity3d.com/Documentation/Components/class-GUIStyle.html

Comment
Add comment · Show 5 · 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 ineteye · Oct 14, 2012 at 09:14 AM 0
Share

No to C#... it was Js code... i converted it but have error in this line:

style.fontSize=24; //NullReferenceException: Object reference not set to an instance of an object winLostNew.OnGUI ()

avatar image Seth-Bergman · Oct 14, 2012 at 09:20 AM 0
Share

ah.. you simply need to set style to the appropriate object, as the error says.. you could simply drag and drop in the inspector

sorry, no, on second look you probably just need to do something like this:

GUIStyle style = new GUIStyle;

avatar image ineteye · Oct 14, 2012 at 09:32 AM 0
Share

So how to do it?? So i just change "GUIStyle style;" to "public GUIStyle style;" and that all?

avatar image Seth-Bergman · Oct 14, 2012 at 09:32 AM 0
Share

hmm, on further investigation I don't think you should have to do that though.. though I would assume the default scope is "public" anyway, you might add that:

public GUIStyle style;

otherwise this should work though..

avatar image Seth-Bergman · Oct 14, 2012 at 11:01 AM 0
Share

right sorry, just noticed your comment.. like this:

public GUIStyle style = new GUIStyle();

as @echofiend correctly points out in his answer, it was the initialization that was the issue.

avatar image
2

Answer by echofiend · Oct 14, 2012 at 10:51 AM

You have to first declare your style as a new style.

 GUIStyle style = new GUIStyle();//Declare it a new object
 
 void OnGUI ()
 {
     style.fontSize=24;
     style.normal.textColor=Color.red;
     style.alignment=TextAnchor.MiddleCenter;
     GUI.Label ( new Rect(Screen.width/2-300, Screen.height/2-20, 600, 40), "TEST",style);
 }

in C# you have to declare a new object before you can use it

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

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

js to C# - Instantiate(scoreParticle, object - Vector3(0,0,0.3), Quaternion.identity) 1 Answer

Need help C# -- yield, destroy 1 Answer

Need help converting js to C 1 Answer

Need help converting js to C# - rigidbody.rotation.z 1 Answer

Enemy AI script causing Unity to crash (Javascript to C#) 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