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 /
  • Help Room /
avatar image
0
Question by $$anonymous$$ · Dec 28, 2016 at 02:27 PM · scripting problemscript.variablepickuphealth

Health pickup script issue

I've used this code to try and create a health potion, however, it doesn't work and I get this error in the Console:

Assets/Scripts/Health potion.js(1,14): BCE0018: The name 'Health' does not denote a valid type ('not found').

You should be able to assign the health control script in the "Script" variable.

 var Script : Health; //This allows you to place the Health script in the Inspector.
      
 function OnTriggerEnter(){
      
     Destroy(gameObject);  // Destroys the health potion
     print("Gained 10 Health");
     Script.player_health +=10; // Script is the Health script we assigned in the Inspector.
        
 }
Comment
Add comment · Show 2
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 tanoshimi · Dec 28, 2016 at 02:31 PM 1
Share

Well, you either haven't got a Health.js class, or there's a problem with it.

avatar image Derpybunneh · Dec 28, 2016 at 03:35 PM 1
Share

Also, the way you have the collisions set up probably won't work. You need to link to a physics collider. (Sorry it's in C#, that's the language I use predo$$anonymous$$antly). You'd do something like this.

 void OnTriggerEnter(Collider other) {
             if (other.gameObject.tag == "hpPotion") {
             // add to player's health
              }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by allenallenallen · Dec 28, 2016 at 02:48 PM

You got the source code from here, right: https://forum.unity3d.com/threads/health-pickup.101377/

If you're copying code from someone else, you better understand how the code works.

Furthermore, if you are going to modify the code you got from someone else, you better understand how EVERYTHING works.

You changed the original "Health Control" script name to "Health". This means you better have a script called Health.js somewhere in your folder. And you better make sure it has a public player_health variable. (In case you haven't noticed, the guy didn't post his "Health Control" script so you should've written your own. In your case, Health.js.)

Then you should think about the code given by the second guy and why it doesn't make much sense. It's destroying the health pickup object along with the "Health potion.js" script that's attached to it before it gives the health to "Health.js".

It should be like this instead:

 print("Gained 10 Health");
 Script.player_health +=10; // Script is the Health script we assigned in the Inspector.
 Destroy(gameObject);  // Destroys the health potion
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 $$anonymous$$ · Dec 29, 2016 at 01:18 PM 0
Share

@allenallenallen I understand the code completely and I have the player_health variable in the Health script. I think the error may be because the Health script is C# and HealthPotion is Js?

avatar image allenallenallen $$anonymous$$ · Dec 29, 2016 at 02:22 PM 0
Share

In that case, yes, the last one that's compiled will be able to see the previous compiled codes.

So I guess Health pickup is compiled first right now.

You can probably fix it by looking at this answer: http://answers.unity3d.com/questions/252865/accessing-a-c-field-from-javascript.html

$$anonymous$$ake sure Health is compiled first.

If that doesn't fix it though, it might be another problem.

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

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

Related Questions

How To Connect 2 Scripts So You Can Communicate The Variables?? 0 Answers

Get value from another script 1 Answer

Script error and no Variables showing 0 Answers

I need help picking up and dropping weapons 0 Answers

how to change material on an object in unity 5 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