- Home /
Communicating between objects in the same prefab?
My question is probably confusing because I don't know the right words, but here's what I want to do...
So I have a tripwire landmine I'm trying to place in my level. When the player hits the tripwire which is also a triggerbox, the wire destroys its self. That's what I have so far. The current script is attached to the tripwire.
function OnTriggerEnter(col:Collider)
{
if(col.gameObject.tag == "Player")
{
Debug.Log("Trap tripped!");
// Tell the warhead to jump and go off.
// Make the tripwire disappear.
Destroy(this.gameObject);
}
}
Now what I can't do is get this script to tell the warhead to do its thing, which is to jump up into the air and explode. I believe I will need a script for the warhead too but I have to know how to set off the warhead in the same prefab and not every single warhead on the damn map.
Almost helped. I'm a novice at this sort of thing.
I do not have a Warhead class. Let me just grab the whole script as it currently stands.
#pragma strict
function Start () {
}
function Update () {
}
function OnTriggerEnter(col:Collider)
{
if(col.gameObject.tag == "Player")
{
Debug.Log("Trap tripped!");
// Tell the warhead to jump and go off.
// $$anonymous$$ake the tripwire disappear.
Destroy(this.gameObject);
}
}
function triggerBomb()
{
// $$anonymous$$ake this jump up into the air. Something something rigidbody.velocity(0,10,0);
// Brief time delay
// Instantiate explosion
}
So imagine the following: A three part prefab consisting of the warhead, a tripwire attached to the warhead, and a stake to secure the tripwire just for looks. All of these are under one GameObject called "Land$$anonymous$$e Prefab".
So I guess in order for this script to work I need to make the warhead a class. I do not know how to do this. Bare with me, I am gooing to have many noob questions.
I assumed you actually had a Warhead class because that's what "Communicating between objects in the same prefab" sounds like. To do exactly what your comments are saying, you just do:
function OnTriggerEnter(col:Collider)
{
if(col.gameObject.tag == "Player")
{
Debug.Log("Trap tripped!");
// Tell the warhead to jump and go off.
triggerBomb();
}
}
function triggerBomb()
{
// $$anonymous$$ake this jump up into the air. Something something rigidbody.velocity(0,10,0);
rigidbody.velocity = new Vector3(0,10,0)
// Brief time delay
yield return new WaitForSeconds(brief_time); //replace with the number of seconds you want
// Instantiate explosion
Instantiate(exposionPrefab);
// $$anonymous$$ake the tripwire disappear. You have to do that HERE, as the stuff after the time delay wouldn't happen if this game object was destroyed.
Destroy(gameObject);
}
Answer by Baste · Oct 22, 2014 at 04:20 PM
You use the GetComponent method to find other components attached to the same GameObject.
So assuming that the warhead has the Warhead class, and the method you want to call is DoYourThing(), the code is:
function OnTriggerEnter(col:Collider)
{
if(col.gameObject.tag == "Player")
{
Debug.Log("Trap tripped!");
// Tell the warhead to jump and go off.
var warHead : WarHead;
warHead = gameObject.GetComponent(WarHead);
warHead.DoYourThing();
// Make the tripwire disappear.
Destroy(this.gameObject);
}
}
Hope that helps!
I'm a boob and can't seem to type properly on this forum. I commented my reply on the wrong answer. Check the comments above for my response.
Your answer
Follow this Question
Related Questions
it is not possible to invoke an expression of type int 2 Answers
Spawn a prefab 1 Answer
Instantiated Prefab's script doesn't work 2 Answers
Two Instances of same Prefab dealing damage to each other. 0 Answers
Prefab Transform variable missing. 1 Answer