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 /
This question was closed Feb 13, 2014 at 10:37 AM by fafase for the following reason:

Duplicate Question

avatar image
0
Question by Ariath · Feb 12, 2014 at 09:17 PM · getcomponentfunctioncall

How to call function from another script ?

Hello

i have a player script with damage simulation :

 private var ennemy : Ennemy ;
 
 function Update ()
 {
 if(Input.GetKeyUp(KeyCode.R))
 {
 ennemy = GameObject.Find("Ennemy").GetComponent(Ennemy) ;
 ennemy.ApplyDamage = 10 ;
 }  


i have a ennemy script with life,armor,and function apply damage :

 var lifeEnnemy : int ;
 var armorEnnemy : int ;
 
 function ApplyDamage (damage : int){
 
    if(armorEnnemy <= 0)
    {
    lifeEnnemy -= damage ;
    }
 
    else if(armorEnnemy < damage )
    {
       var diff : int ;
       diff = damage - armorEnnemy ;
       armorEnnemy -= armorEnnemy ;
       lifeEnnemy -= diff ;
    }
    else
    {
    armorEnnemy -= damage ;
    }
    }


But i want creat a new script : "global" with lifeEnnemy and armorEnnemy and function applydamage.And modify my ennemy script in :

 private var ennemy : GlobalVar ;
 
 function Start ()
 {
 ennemy = GameObject.Find("Player").GetComponent(GlobalVar) ; 
 ennemy.lifeEnnemy = 50 ;
 
 ennemy = GameObject.Find("Player").GetComponent(GlobalVar) ; 
 ennemy.armorEnnemy = 50 ;
 }

+ my function apply damage

So i could make diferent ennemy with different life and armor.

i know how to call a variable with get component but i don't know how call a function ?

Hope it s clear ?

Thank s !

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 flamy · Feb 13, 2014 at 10:33 AM 2
Share

this question have been asked 1000s of times, for god sake please google first before posting http://bit.ly/1kDYN$$anonymous$$C

1 Reply

  • Sort: 
avatar image
0

Answer by Mysta · Feb 12, 2014 at 09:42 PM

I could be mistaken but i believe at the beginning you would do something like the following:

 //if the script with applydmage is called ScriptWithDamage.cs
 private ScriptWithDamage damageScript;
 
 //in awake
 void Awake()
 {
 damagescript = GetComponent<ScriptWithDamage>();
 }
 
 //Then when you want to call it
 damagescript.ApplyDamage(10);
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 Ariath · Feb 13, 2014 at 10:29 AM 0
Share

i don t understand, i must use awake function to call my applydamage function ?

(sorry i work with javascript)

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Call a function multiple times 2 Answers

StartCoroutine important for using yield? 1 Answer

Building a 3D Neural Network for Bot AI? 1 Answer

How to call a function from another script without referencing it? 1 Answer

NullReference when calling a function to another script 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