- Home /
Issues with one object receiving information from one of many other objects
I'm having an issue connecting a projectile to a turret in my game.
I am currently using a one-to-one relationship model between a projectile and the turret it came from. The turret send information (e.g. damage to enemies) to the projectile, and the projectile receives it. However, I'm having trouble figuring out how to get the projectile to actually receive the information from the turret.
The main problem lies within how the turrets behave in my game. Each turret has an upgrade system included which increases the amount of damage a projectile inflicts on a target. Each turret upgrades independently, however; given the one-to-one relationship mentioned earlier, this means that turret A could fire a projectile that inflicts more damage than turret B if A has upgraded more than B.
I currently have a script attached to the projectiles which I initially thought would receive information using a reference to a script attached to each turret in the game. However, the projectile's scripts has no way of differentiating which turret it came from.
If anyone has any idea as to help, please let me know. Thank you for taking the time out to read this.
Answer by robertbu · Dec 17, 2013 at 06:12 AM
The easiest way would be to have the turret set the parameters of the projectile as it is launched. The code might look like:
var go = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
var projScript = go.GetComponent(ProjScript);
var projScript.damageFactor = damageFactor;
var projScript.maxSpeed = maxSpeed;
Where 'damageFactor' and 'maxSpeed' are changed/upgraded by the turret.
Another solutions is to have a unique tag associated with each turret that is assigned to the projectiles. Then you can use GameObject.FindGameObjectsWithTag() to get a list of all the projectiles fired by one turret.
And another solution to have a 'fromTower' variable on the projectile script that uniquely identifies what tower each projectile comes from. You would first get a list of all projectiles using FindGameObjectsWithTag(), then you could cycle through them and change the values of any with a specific tower tag.
And there are many other ways to go about your task. Note many games implement a object cache so they can reuse things like projectiles rather than use expensive Instantiate() and Destroy() calls. If you do this, then you can cycle through the entries in your cache.
I'm still having trouble internalizing your solution, so I've created a general scenario to help me out: each turret has a cache of n projectiles. Once one projectile from that cache is fired, it is given a tag "FiredProjectile." Projectile P, upon hitting its target, has to run a function which gives turret T, the turret P is fired from, experience points. This function, however, is located on a script attached to T that P is fired from.
How do I get P to run a function connected to T?
This is yet a different solution. Since I don't have your code, let's assume that the script attached to the turret is called 'Turret' and the one on the projectile called 'Projectile':
So at the top of your projectile script, you will have:
var turret : Turret;
Then when you launch the projectile:
var go = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
var projectile = go.GetComponent(Projectile);
projectile.turret = this;
Note I'm assu$$anonymous$$g here that the turret is the one launching the projectile. If it is not, then you will need to use GetComponent() to initialize 'projectile.turret'.
When the projectile hits, you can then use the 'turret' variable to access anything public in the turret script that launched the projectile. This includes functions.
Note this is not my original suggestion, but it may be what you need. $$anonymous$$y suggestion was have the turret set the experience points in the projectile as it was fired.