- Home /
Trouble with sendmessage
I've got this in a script attached to a rocket launcher: (FYI, Projectile is a public variable of type GameObject, I set it by dragging the prefab of a specific type of projectile to the inspector).
Instantiate(Projectile, transform.position + ProjectileOffset, transform.rotation);
Projectile.transform.LookAt(_target.transform);
Projectile.SendMessage("SetTarget", _target, SendMessageOptions.DontRequireReceiver);
This creates a projectile at the launcher location, points it at the target, and tries to let the projectile know who its target it.
In the Projectile GameObject's projectile script, I have this:
public void SetTarget(GameObject target)
{
Debug.Log("Setting target to " + target);
Target = target;
}
For some reason, this never gets called, even though the projectile is being created and "launched". Any ideas on why this method isnt getting called, or perhaps a different approach to take?
Answer by Eric5h5 · Mar 28, 2010 at 04:48 AM
You're trying to SendMessage to the actual prefab. You need a reference to the instantiated clone that exists in the scene:
GameObject projectileClone = (GameObject)Instantiate(Projectile, transform.position + ProjectileOffset, transform.rotation);
projectileClone.transform.LookAt(_target.transform);
projectileClone.SendMessage("SetTarget", _target, SendMessageOptions.DontRequireReceiver);
As an aside, I'd recommend sticking to the convention of using lowercase for variable names and upper case for classes and types, because otherwise it gets confusing as to what you're referring to.
Your answer
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
Functions work in the Editor, don't work when Built. (Javascript) 0 Answers
why wont this SendMessage work 1 Answer
.sendmessage not working C# 1 Answer