- Home /
The question is answered, right answer was accepted
Finding a script on an object
Hi,
I have the following code which works, but I was wondering how it could be modified to find the DamageReceiver script if it was on the parent of the object that the projectile hit.
e.g.
I shoot the spaceship - I can find the script
I shoot the shield - I cannot – but is there a way I could?
function OnCollisionEnter( collision : Collision )
{
// find out where and what we hit
var contact : ContactPoint = collision.contacts[0];
var rotation = Quaternion.FromToRotation( Vector3.up, contact.normal );
// visual effects
var instantiatedExplosion : GameObject = Instantiate(explosion, contact.point, transform.rotation);
// tell the other object that they have been hit and how badly
if (contact.otherCollider.GetComponent(DamageReceiver))
{
contact.otherCollider.GetComponent(DamageReceiver).attacked = true;
Answer by DaveA · Jul 24, 2012 at 12:48 AM
You might instead want to use SendMessageUpwards http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessageUpwards.html which will send the message also to the parent(s)
I decided against this due the the amount of information I need to send.
I also could not get it to send a javascript object containing the information to be accessed by the receiving script
Are you sure? That API takes type 'object' so it should take anything I would think, structs or classes.
You can also crawl the tree yourself by accessing otherCollider.transform.parent and going up that.
it was sending but the content was unacessable on the other end. I will try the tree crawl thing, but if you could give me a working example of sending and retrieving data using an object that would be great.
Follow this Question
Related Questions
Update Parent/ Children From Script? 2 Answers
Changing the parent of an object in a script 2 Answers
Character falls through the ground 1 Answer
GUI.Box within a Collider 1 Answer
i cant get an object to become child of an other JS 1 Answer