Basically what I’ve done so far is that when my player touches a certain window in the game,
it sends out a message called “bomb”.
Then I made a new script with the function “bomb” (the message sent out), so when it receives the message the bomb animation should play.
—but it hasn’t seemed to be working, please help me.—
This is the Players Scripts
using UnityEngine;
using System.Collections;
public class PlayerReaction : MonoBehaviour{
public GameObject myGameObject;
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Start Window")
{
gameObject.SendMessage("Bomb");
}
}
}
And this is the Bomb Script,
using UnityEngine;
using System.Collections;
public class BombReaction : MonoBehaviour {
function Bomb{
gameObject.animation.play
}
}
A better way of doing this would be to access that script directly, so instead of sending the message you just access the “function” Bomb. Put your BombReaction script on the window object.
Then reference that object, I’ll keep it simple and just use a public GameObject that you can drag onto the slot in the inspector just like you do already.
using UnityEngine;
using System.Collections;
public class PlayerReaction : MonoBehaviour{
public GameObject myGameObject;
private BombReaction myBombReaction;
void Start()
{
myBombReaction = myGameObject.GetComponent<BombReaction>();
}
void OnTriggerEnter (Collider other)
{
Debug.Log(other.gameObject.tag); // use to check for spellin errors etc
if (other.gameObject.tag == "Start Window")
{
myBombReaction.Bomb();
}
}
}
Then fix the little errors in your BombReaction script:
using UnityEngine;
using System.Collections;
public class BombReaction : MonoBehaviour {
public GameObject myBomb;
public void Bomb(){
Debug.Log("Should be playing the bomb animation");
myBomb.animation.Play();
}
}
broadcasting a message is a bit wasteful although perfectly acceptable way of doing things and you need to be extra careful with things like brackets as C# is very strict.
And make sure you drag the gameObject onto the slots in the the inspector.