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 Marvin Hawkins · May 11, 2012 at 12:53 PM · guivarible

Trouble Accessing Variables from another gameobjedt

Hello,

I am trying to create a simple GUI script that holds all of the GUI elements for the game. I am starting with just a player health bar, which requires me to call in variables from the "PlayerScript". I'm using a .Getcompenent to do this. But I either get a "behavior null or not found' or the game throws back no errors, but runs really slowly. Below is my code. Where am I making the mistake? Playerfear is a variable from the script: PlayerController.

Sidenote, I realize that none of this was displaying on screen, so I created a guicontroller game object, placed it in my scene, and attached the script. The GUI texture, nor the box were visible, and I kept getting "Null Errors" I'm not sure why. Any help you can provide is greatly appreciated.

     public var pScript: PlayerController;// since this is pragma strict the type of varible must be stated strictly.  the varible will not be a "component" persae,but  
                                      // a type of compontent called PlayerController. long story short, if you do a pragma strict, the code will be more strict                                          
                                     //in typesetting of variables. Don't do pragma strict if you aren't  comfy with this.
 public var fearTexture: Texture2D ;
 
     function Awake () {
           //removed var from infront of pScript since we declared it already
      pScript= gameObject.FindWithTag("Player").GetComponent(PlayerController); //First find the player object and assign it to a variable
         
         print(pScript.playerFear);
         }
         
     
     function OnGUI () {
     
     
         GUI.DrawTexture(Rect(10,10,(Screen.width/2)*(( pScript.playerFear+1.0)/(1.0*pScript.maxFear+1)),20), fearTexture, ScaleMode.StretchToFill, true, 10.0f);
         //GUI.Box(new Rect ( 10, 10, (Screen.width/2),20),(pScript.playerFear)+"/"+(pScript.maxFear) );  //This is its on rectangle
         
     };
     
     
     
     function Update()
     {
     print(pScript.playerFear);
     }
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

1 Reply

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

Answer by Datael · May 11, 2012 at 01:06 PM

I might be wrong but it is probably because you are trying to access pScript during Awake() and it may not have initialized yet. Try changing Awake to Start to see if that helps at all. As for the speed there's nothing in this script (unless it's the DrawTexture but I doubt it) that would make it slow that I can see; you're caching all the components properly etc from what I can tell, so it might be something somewhere else that's causing this.

Finally, if I may, I recommend that you sort out your indentations; it will make your life a lot easier if you get into the habit of making sure that everything is indented properly. As it is now, at first glance it looks as though all those functions below are members of fearTexture (obviously they aren't, but the indentation makes it look that way.)

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 Marvin Hawkins · May 18, 2012 at 11:18 AM 0
Share

Thanks! That did the trick. I kind of have a similar situation for a texture display. For some reason, this method does not work for displaying a graphical image for player lives. Any suggestions on where to look?

avatar image Datael · May 19, 2012 at 01:44 PM 0
Share

I'd have to have a look at what exactly you mean; there's quite a number of different ways to display a texture so to narrow it down a bit could you put up some code, please? One common thing between all of the ways that could potentially be the problem is that the Texture2D will definitely need to have been initialized before you try and access it.

What error message are you getting? Or does it just not display?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

What is the best way to define different GUI pages? 2 Answers

Why cant I scroll Horizontally in GUI.BeginScrollArea when i set GUIStyle params to null ? 1 Answer

[SOLVED] Object detection inside a GUI area 1 Answer

Rendering GUI.Buttons in the middle of the screen 5 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