- Home /
Homing Missile Target From Parent - Help
Hello,
I have a missile launcher turret that fires 'homing' missiles. By homing, I mean that the missile that is initiated is suppose to retrieve a 'target' that is defined by the missile turret. I need the turret to define the target in 'projectile' (missile) without using static variables.
So what happens right now: enemy walks into radius of missile turret. This enemy is then added to a target variable. I now need this variable to be passed onto the projectile target.
Note: When missile initiates, it is not initiated as a child (so I can't use script = GetComponents -- or can I?).
I don't mind if the turret sends the target to projectile, or projectile receives target from turret.
Summary:
Missile Turret > Enemy Added to variable > projectile also adds same target to its variable.
Answer by Statement · Dec 19, 2010 at 10:34 PM
Note: When missile initiates, it is not initiated as a child (so I can't use script = GetComponents -- or can I?).
I don't know what you mean by that, but you can access components of any game object. There shouldn't be anything holding up from accessing scripts on your projectile object. It doesn't have to be a child to yourself. Simply do myProjectile.GetComponent("Projectile"); Then you can just simply set the target once you have the script reference. Another thing you can do is to broadcast the target, in case you don't know in which child the component is, or if several components should respond to a common event.
I don't mind if the turret sends the target to projectile, or projectile receives target from turret.
Your turret should probably send the target to the projectile.
I keep getting a Null - or Broadcast has no receiver?
It's because its not attached to the same game object - "To access public variables and functions in another script attached to the same game object."
If your broadcast have no receiver, you haven't written a receiver. Write a receiver. And you might want to send the message with no receiver require options (once you get it working).
If its not attached to the root, you can always find it through GetComponentInChildren()
Still not working, I think its because: I have a smoothLookAt script that detects nearby enemy > A missileLauncher script that receives target from smoothLookAt and fires projectile with mouse click > the projectile is then initiated with the projectile script attached to it - this needs to receive target from missileLauncher. But because its initiated - how would I go about telling the projectile the target?