Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Eli-Davis · Jun 05, 2011 at 08:07 PM · playergetcomponentreferencelevel

getting a variable from another script problem

Alright, so I made a script and attached it to an object, and when ever I shoot the object, it gets the level of the player and multiplies it by 1 (which now that i think about it, thats pointless). any way once health hits zero, the object explodes. This all works. But its also supose to add experience referncing the players script. but it doesn't. I've tried restarting unity and everything. So I'm wondering if theres something wrong with my script. here it is:

 var health = 5.0;
 var explosion:Transform;
 
 function OnCollisionEnter(hit:Collision){
     var level = gameObject.Find("Player").GetComponent(Level);
     var playerLevel = level.level;
     if(hit.collider.tag == "Fire Ball"){
         health -=1.0 * playerLevel;
         checkhealth();
     }
 }
 
 function checkhealth(){
     var level = gameObject.Find("Player").GetComponent(Level);
     var playerExp = level.experience;
     if(health<=0.0){
         playerExp += 50.0;
         Instantiate(explosion, transform.position, transform.rotation);
         Destroy (gameObject);
     }
 }
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
2
Best Answer

Answer by Bunny83 · Jun 05, 2011 at 08:16 PM

Your experience variable is of type float (which is a value-type, not a reference type like a class). this line just copies the value into a temporary variable:

 playerExp = level.experience;

If you change the temporary variable the variable in your level script won't change. Use either directly level.experience += 50.0 or assign the temp variable back to level.experience.

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 Eli-Davis · Jun 05, 2011 at 08:24 PM 0
Share

Thank you...so much.. I've been trying to figure this out for about 15 $$anonymous$$utes. I'm still relatively new to JavaScript. I don't know what a float variable is XD

avatar image save · Jun 05, 2011 at 09:30 PM 0
Share

Float is short for floating point which is a decimal value (example: var three : float = 3.14;). An int (integer) is a number without decimal (example: var three : int = 3;). Basically they are the two user variables used together with calculations. Basic text is called a string which is set by quotations (example: var three = "3";).

To convert between them you can use:

numberVariable.ToString()

parseInt(stringOrFloatToIntVariable)

$$anonymous$$athf.RoundToInt(floatToIntVariable) <- (this one has lots of options)

avatar image Eli-Davis · Jun 05, 2011 at 09:50 PM 0
Share

thank you xD would you be mad If i put that on my blog? If you want I can say I got it from you and give people a link to your unity answer acount

avatar image save · Jun 05, 2011 at 10:04 PM 0
Share

Sure go ahead! Glad to help.

avatar image Eli-Davis · Jun 05, 2011 at 10:07 PM 0
Share

thank you.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Referencing booleans from settings 1 Answer

Reference Not Set toInstance of Object 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Script works for NPCs, but not Player 1 Answer

Getting NullReferenceException? info inside 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