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 convictcartel · Jan 09, 2014 at 11:42 AM · javascriptc++getcomponet

Cant Get Componet From Script

I am having a small issue using the componet command from my C## script, my current script is javascript and I want the script to add health by using this command inside of my "PlayerHealth.Cs" file.

 AdjustCurrentHealth(+10);

Both of my scripts are attached to my player but when I tried to use

 var Script : PlayerHealth;

I get an error saying it cant find my PlayerHealth file.. so please and thank you! Stuck :/

 GUI.skin = HEALTHPOT;
         if (bagopen == true) {
         if(GUI.Button (new Rect(Screen.width - Screen.width/8, Screen.height - Screen.height / 3.6, 64, 64), healthpots + "")) {
        if ( healthpots >= 1) {
         healthpots --;
         
         }
         }
         }
Comment
Add comment · Show 1
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 AlucardJay · Jan 10, 2014 at 03:57 AM 0
Share

http://answers.unity3d.com/questions/385582/how-to-connect-2-scripts-using-c-js.html

1 Reply

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

Answer by Invertex · Jan 09, 2014 at 12:07 PM

If the script is on the same GameObject, then GetComponent in C# is fairly simple.

First you start by declaring a Component variable, this is the structure every time:

 [modifier] [component name] [name we want this variable]

Like so:

 private PlayerHealth playerHealth;

But that is not enough. We also need to declare on Start or Awake, that playerHealth is equal to the component we want. So we do:

 playerHealth = GetComponent<PlayerHealth>();

And that's it! Now you can use the playerHealth component/script in your other script.

If the PlayerHealth script is not on the same object as this script, then you need to query the object it's on and GetComponent. One way is to do the same we did here, but on Start/Awake, we:

 GameObject.Find("name of object PlayerHealth is on").GetComponent<PlayerHealth>();
Comment
Add comment · Show 4 · 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 convictcartel · Jan 10, 2014 at 03:07 AM 0
Share

This works perfect in C## but the script im using the code in is a javascript code! Can you tell me how to use this on javascript? Thanks !

avatar image Invertex · Jan 11, 2014 at 06:48 AM 0
Share

I see you accepted the answer... Sorry I couldn't be more help, I rarely touch JS. Did you just convert the script to C#?

avatar image edcarlo · Jan 11, 2014 at 11:32 AM 0
Share

GetComponent("PlayerHealth"); // for js GetComponent(); // for c#

avatar image Invertex · Jan 11, 2014 at 11:57 AM 0
Share

@edcarlo, looks like Unity strips the greater and less than signs sometimes haha.

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

21 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

Related Questions

boolean questions get, set, in java. 1 Answer

How to get objects with a specific component and store them in an array. 2 Answers

Steps to create service tool with unity api 1 Answer

Javascript passing pointer to c++ DLL, 0 Answers

Script for getting in and out of a tank 2 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