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 Firgof · Jun 04, 2013 at 12:14 AM · prefabinspectorvariablespublicinstancing

Calling individual variables from individual scripts

In the Inspector each script has a different pair of values for Note1aTexture and Note1bTexture but in-game I can only ever pull up one texture -- no matter how specific I am about referring to this script that's being run from the object that just collided with the player. What am I doing wrong or failing to comprehend?

 private var hasCollided: boolean = false;
 private var usedState: boolean = false;
 var labelText: String = "";
 var Note1aTexture: Texture;
 var Note1bTexture: Texture;
 var pickupSound: AudioClip;
 var pgWidth = Screen.width/2;
 var pgHeight = Screen.height/1.5;
 var pgW2 = pgWidth/2;
 var pgH2 = pgHeight/2;
 var pgX = 0;
 var pgY = 0;
 var pgMargin = 32;
 
 
 function Update(){
     if (hasCollided == true){
         if (Input.GetKeyDown(KeyCode.E)){
             if (this.usedState == true){
                     Destroy( transform.root.gameObject );
             }
                 
             if (this.usedState == false){
                 this.usedState = true;
                 print("Player picked up item.");
                 audio.PlayOneShot(pickupSound);
                 if (tag == "Note"){
                     print("It was a note.");
                 }
             }
         }
     }
 }
  
 function OnGUI()
     {
     if (usedState == true){
         GUI.DrawTexture(Rect(pgX,pgY,pgWidth,pgHeight),this.Note1aTexture,ScaleMode.StretchToFill,true,0.0f);        
         GUI.DrawTexture(Rect(pgX+pgMargin*2,pgY+pgMargin,pgWidth-pgMargin*8,pgHeight-pgMargin),this.Note1bTexture,ScaleMode.StretchToFill,true,0.0f);
         GUI.Box(Rect(140,Screen.height-50,Screen.width-300,120),("Press E to add to Journal; Space to put it down."));
         
         if (Input.GetKeyDown(KeyCode.Space)){
             this.usedState = false;
             audio.PlayOneShot(pickupSound);
         }        
     }
     if (this.usedState == false){
        if (this.hasCollided ==true){  
               GUI.Box(Rect(140,Screen.height/2,Screen.width-300,32),(labelText));
         }
     }    
 }
  
 function OnTriggerEnter(c:Collider){
     if(c.gameObject.tag =="Player"){
         this.GetComponent.<PickUpNote1>().hasCollided = true;
     }
 }
  
 function OnTriggerExit( other : Collider ){
     this.GetComponent.<PickUpNote1>().hasCollided = false;
 }
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 Benproductions1 · Jun 04, 2013 at 12:16 AM 0
Share

Just FYI, you can't call variables, unless they point to a function :)

avatar image Benproductions1 · Jun 04, 2013 at 12:17 AM 0
Share

This should work, what exactly isn't going to plan?

avatar image Benproductions1 · Jun 04, 2013 at 12:18 AM 0
Share

BTW If you have 2 scripts of the same type attached, bad things generally happen

avatar image Firgof · Jun 04, 2013 at 12:26 AM 0
Share

I have the same script attached to multiple objects. When I try to get information from one of those scripts, filtered by the collision volume the player is touching, I ins$$anonymous$$d get whichever the engine decides (which I believe is always the last instance of the script).

For instance, obviously hasCollided should only become true on the object which has been collided with; unfortunately it's being set to both scripts. I need some means to discri$$anonymous$$ate both ways here but I don't know of any.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by darthbator · Jun 04, 2013 at 12:17 AM

This is JS? In C# when you say this.var or this.method you are saying basically "my instance, the one I am executing right now". What texture are you trying to get? Are you trying to grab a texture stored on a script in the item you are picking up?

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 Firgof · Jun 04, 2013 at 12:24 AM 0
Share

Correct. I'm wanting to draw the textures defined in the Inspector of the script attached to the object the player has collided with.

Ins$$anonymous$$d, I'm only getting one script's textures and both scripts are getting set whenever only one should when the player collides with the trigger volume.

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

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

Related Questions

what public variables can be shown and changed in the inspector 1 Answer

Problem exposing public variable to Inspector in C# 2 Answers

When adding a script using GetComponent<>(), some prefabs that are assigned don't come up 1 Answer

Prefab not loading in data from Inspector 1 Answer

Variables in editor showing in a wrong way 0 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