x


Refferencing to other scripts

Hello,

I'm having trouble trying to make this and I don't really know how to do it.

What I'm trying to accomplish is this.

I've got a wooden box, and a character, the wooden box has a script named "Destruible" (it means that it can be destroyed, it's not in english, i don't think it matters really much so i kept it untranslated), and my character's sword has a script named SwordCollision.

The SwordCollision script chechs the collisions that the sword receives. And what I'm trying to do is check when the sword collides with a object that can be destroyed (checking its Tag) and also check if the character is attacking.

If both of them are true, I try to access the other object attached script named Destruible, and access the function in it named MakeDestroy, so the object will be destroyed, and a particle explosion would be placed instead (This is what MakeDestroy function makes)

The problem is that I don't know how to access other's Script that has attached and execute one of it's functions (other collider, in the function OnTriggerStay), here's the code I'm trying to make, it isn't working right now:

 function OnTriggerStay (other : Collider) {
 
 if(MonkeyEngine.attacking && other.CompareTag ("Destruibles"))
 {other.gameObject.Destruible.MakeDestroy();}//The problem is in this line
 }

Well I don't really know how to reffer to the function of the script from the other collider object but this isn't the right way, so if anybody can help me, I'd really appreciate it.

Thank you

more ▼

asked Aug 04, 2011 at 01:26 PM

Setzer22 gravatar image

Setzer22
571 118 96 110

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Once you have some reference to the object, you can get the script by type - which for scripts is the filename without quotes or extension:

function OnTriggerStay (other : Collider) {

if(MonkeyEngine.attacking && other.CompareTag ("Destruibles")) { other.GetComponent(Destruible).MakeDestroy(); //The problem WAS (I hope) in this line }

more ▼

answered Aug 04, 2011 at 01:46 PM

aldonaletto gravatar image

aldonaletto
57.7k 33 94 305

Thank you, that completely solved my problem, I knew what I wanted to do, it was just i didn't know how to do it exactly, now I saw I had to use the GetComponent function

Aug 04, 2011 at 02:18 PM Setzer22
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4931
x2996
x68

asked: Aug 04, 2011 at 01:26 PM

Seen: 1158 times

Last Updated: Aug 04, 2011 at 02:18 PM