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 10, 2012 at 07:14 AM · javascriptguigetcomponent

create GUI.Label and then access to it..

Hi! So need to create GUI.Label and then access to it...

first script :

test.js

Code:

 var style : GUIStyle;
 
 
 public var SCORE = "0";
 
 
 
 function OnGUI () 
 
 
 {
 
 
     style.fontSize=18;
 
 
     style.normal.textColor=Color.red;   
 
 
     GUI.Label (Rect (Screen.width*0.2, Screen.height*0.15, 100, 20), SCORE,style);
 
 
 }

test_access.js

Code:

 function Start()
 
 
 {
 
 
     var LOAD : test;
 
 
     LOAD = GetComponent(test);
 
 
     LOAD.SCORE="1";
 
 
 }

test and test_access - both different object.. error NullReferenceException: Object reference not set to an instance of an object

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
0

Answer by T27M · Oct 10, 2012 at 07:34 AM

If you have the scripts on a different object you have to tell it which object to look for.

 var myObject = GameObject.Find("ObjectsNameHere");
 
 LOAD = myObject.GetComponent(test);
 
 
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 whydoidoit · Oct 10, 2012 at 07:34 AM

You need something in test_access.js that refers to the component with test.js on it - then call GetComponent on that - or just assign it in the inspector:

test_access.js

   var theOtherObject : test;
 function Start() {
 
      theOtherObject.SCORE = "1";
 
 }
 

Or:

 var otherObject : GameObject;
 
 function Start() {
 
     otherObject.GetComponent(test).SCORE = "1";
 
 }

Please remember naming conventions really help - your scripts (and their inherent classes should start with a capital). Variables should start with a lower case letter and then be Camel case. All upper case implies a constant or preprocessor defined value.

Comment
Add comment · Show 1 · 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 10, 2012 at 09:29 AM 0
Share

Thanks...

Get something like that to work:

 public var test_obj :GameObject;

 

 function Start()

 {

     test_obj.GetComponent(test).SCORE="1";

 }





So there some question... if i want to shorten line test_obj.GetComponent(test).SCORE

local object SCORE, like

 SCORE=test_obj.GetComponent(test).SCORE;

 SCORE="1";


what object should be SCORE mean

var SCORE : ???????

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

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Limit on GUI Components? 0 Answers

How To Make Ammo & Realod for Gun & Spark for Gun ? 0 Answers

[Solved] Error when trying to add a boolean array variable to GUI Toggle 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