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 davidc · Apr 29, 2013 at 12:28 PM · javascriptvariablescriptingbasicsfunctions

Creating & accessing a function from a diffrent "OnTriggerEnter" Function

Hey guys, so i am having allot of trouble with this.

I had a function on my "player" that whenever it collided with a enemy, health would be taken off, it worked fine untill my player remained still and the enemys ran into him, in which nothing would happen. Thanks to you guys someone pointed out that it was because the "enemy" was colliding with the "player", not the other way around.

so the solution would be to create a function on the "enemy" that activated the function on my main script (attached to my player) so that the important code can be executed and my character can loose health.

does anyone know the basics? i have looked up heaps of answers about this but i unfortunately still cant get it to work.

does anyone know the code to access another function in another code and thence forth activating that function? how would i write the function in the "main code" after i have successfully gotten my "enemy code" to access it?

Dave...

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
1
Best Answer

Answer by ExTheSea · Apr 29, 2013 at 04:20 PM

Hey Dave. So we meet again :)

What you could do is something like this:

In the Enemy-Script:

 function OnTriggerEnter(collision : Collider){  
   if(collision.gameObject.tag == ("Player")){ 
     collision.gameobject.SendMessage("CauseDamage");
   }
 }

The method SendMessage calls a function in the script of another object. Here is a link to the script reference: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html

and in the Player-Script you posted in the other question you add this:

 function CauseDamage(){
   BurgersGui.Burgers +=BurgerNegative;
   transform.localScale += Vector3(-0.1,-0.1,-0.1);
 }

I hope this works for you and if not just ask.

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 davidc · Apr 30, 2013 at 03:16 AM 0
Share

hahahaha, we do meet again, you have helped me so much, thankyou. ok so i will give your code a try. thankyou so much, legend.

avatar image davidc · Apr 30, 2013 at 03:31 AM 0
Share

haha, we do meet again, thankyou for all your help. so i tried it but i get an error on the following line "collision.gameobject.Send$$anonymous$$essage("CauseDamage");"

the error says "Object Reference not set to an instance of an object"

avatar image davidc · Apr 30, 2013 at 05:11 AM 0
Share

i figured it out, i had to use this if(collision.gameObject.tag == ("Player")){ gameObject.Find("Player").Send$$anonymous$$essage("Damage");

you were still right though

avatar image davidc · Apr 30, 2013 at 05:15 AM 0
Share

Im not sure what to do here though, the script works fast and causes dammage way to fast, how can i make my character invincible a few seconds the damage?

avatar image ExTheSea · Apr 30, 2013 at 04:21 PM 1
Share
 private var nextDamageTime = 0.0;
 var DamageRate = 0.05;
 
 function CauseDamage(){
 
  if (Time.time - DamageRate > nextDamageTime)
   nextDamageTime = Time.time - Time.deltaTime;
 
  while( nextDamageTime < Time.time) {
 
     //Do your damage stuff
 
   nextDamageTime +=DamageRate;
  }
 }

This is a part of my FPS Gun-Script but it should work in your case (maybe you can even replayce the while with an if).

Let me know if it works.

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

13 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

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

Unity3d Javascript Reference Script Variable 1 Answer

Resticing variables to whole numbers 3 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