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 vipvex · May 04, 2012 at 01:46 AM · variableaccessingother

Problem with accessing variable form other scripts.

I'm trying to make a button that makes a variable in another script true but i get this error every time. Please help. Code:

Error: NullReferenceException: Object reference not set to an instance of an object Gui.OnGUI () (at Assets/Scripts/Gui.js:29)

     #pragma strict
 
 var moveButtonHight : int;
 
 var moveButtonWidth : int;
 
 var attackButtonHight : int;
 
 var attackButtonWidth : int;
 
 var tempName : String = "hi";
 
 public var CreateCharacter : boolean = false;
 
 
 function Start () {
 
 }
 
 function Update () {
 
 }
 
 function OnGUI() {
     
     if (GUI.Button(Rect(Screen.width / 2, Screen.height - moveButtonHight, moveButtonWidth, moveButtonHight), "Move")){
         
         Debug.Log("Clicked move");
         var player = GameObject.Find("player").GetComponent(player).Moving;
          //var com = player.GetComponent(player);
           player = true;
     }
 
     if (GUI.Button(Rect(Screen.width / 2 - attackButtonWidth, Screen.height - moveButtonHight, attackButtonWidth, attackButtonHight), "Attack / Defend")){
         
         Debug.Log("Clicked Attack / Defend");
     
     }
 
     if (Input.GetKeyUp(KeyCode.C)){
             
             Debug.Log("creating character");
             
             tempName = GUI.TextArea (Rect (Screen.width /4 , Screen.height / 4, 200, 100), tempName, 16);
             
         }
 
 }

 
Comment
Add comment · Show 4
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 hijinxbassist · May 04, 2012 at 02:31 AM 0
Share

Does it throw a null without the var com... line? If so looks like the Component "player" is null, or maybe it just need to be capital "Player"

avatar image Noah-1 · May 04, 2012 at 03:08 AM 0
Share

Try getting the var every frame, put GetComponent in the update function, maybe that helps.

avatar image syclamoth · May 04, 2012 at 03:09 AM 0
Share

Try rewriting it so that you don't have to use GameObject.Find for anything. You should never have to use that function.

avatar image Graham-Dunnett ♦♦ · May 05, 2012 at 04:16 PM 0
Share

Just a random thought, maybe call your javascript something else other than Gui, since that might be confusing the script compiler. Also, what is line 29 in your code?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Piflik · May 05, 2012 at 05:37 PM

This is not the problem throwing the error message, but the 'var player...' line is probably not doing what you think it is doing. You assign the new player variable the value of the Moving variable in your player script (probably False), not the variable itself. Changing player to True will not affect the Moving variable in the player script.

Regarding the Error: be sure to pay attention to case sensitivity. Player != player. Also try GameObject.FindWithTag...I think I remember having problems with GameObject.Find...

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

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Accessing A Variable From Another Script 4 Answers

Accessing another var on another script 1 Answer

accessing a variable from another script. 3 Answers

is this correct way to change var from other script? 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