- Home /
instantiate prefab and link 3rd party gameobject script
Hi so basically I have a script that shoots but I want players to have stats and guns to have their own damage
So I have the bullet that is instantiated and the bullet works out the damage it does but I want it to work out the damage using the gun it is shot from and the person that shot it
Hierarchy example
Player1 > Character > Weapons > Assault_Rifle
I have a script on Assault_Rifle
if (Input.GetButton("Fire1") && Time.time > nextShot && clipBullets > 0 && curReload == false) {
nextShot = Time.time + gunStats.fireDelay;
Rigidbody bulletInstance;
bulletInstance = Instantiate(wepBullet, gunBarrel.transform.position, gunBarrel.transform.rotation) as Rigidbody;
bulletInstance.AddForce(gunBarrel.transform.forward * bulletSpeed);
gunBarrel.GetComponent<AudioSource>().Play();
clipBullets = clipBullets - 1;
ammoText.text = (clipBullets + " / " + gunStats.clipSize);
}
script for weapon damage is on Assault_Rifle, script for % bonuses against species is on Character
Answer by xortrox · May 12, 2015 at 10:39 AM
make a Bullet script that has a public damage variable to set, then after Instantiating the bulletInstance just do:
Bullet b = bulletInstance.AddComponent();
b.Damage = 100.0f;//example value
Same concept if you have a "piercing effect"
b.PiercePercentage = 0.5f;
or simply a damage type enum
b.DamageType = DamageType.Piercing;
As for the stats part you would have to elaborate on how the stats would work or even be saved (per game?, hiscores? etc).
Stats are saved through the game like a leveling system, example... Character has a stats script and is referenced in the bullet_Script
private players_Stats refPlayerStats;
if (colObjScript.species == "$$anonymous$$echanical") {
print ("Is $$anonymous$$echanical");
if (refPlayerStats.mechanicalExpert == true) {
totalDmg = totalDmg + (totalDmg / 10);
colObjScript.curHealth = colObjScript.curHealth - bulletDmg;
}
But using your idea... $$anonymous$$aybe it would be better to have the stats on the actual bullet and when the gun instantiates it, it sets the dmg and if the bullet has mechanical dmg bonus...
So ins$$anonymous$$d of the bullet needing to reference everything everytime it spawns, the gun references it and adds it to the bullet
If all the guns are prefabs then I can simply get the object of character by referring to the guns parent? can Instantiate weapons as a child of the character that way
I assume you would be instantiating the gun as well and this would probably be done from the character object script (or at least how I would do it). As for your damage issue the gun should probably assign the damage for the bullet based on the player having the mechanicalExpert trait yes. Stats should remain on the character object as well I assume.
my usual ideal setup: Player class instantiates the weapons thus getting their reference for adding components and what not, and usually I pass a reference to the weapon for a "ParentPlayer" variable or just "Player Parent".
Weapon class instantiates the bullets which have specific data like the damage they will deal or even the damage type they would deal (which is deter$$anonymous$$ed by the Parenting player for the weapon. So for instance:
public class Player : $$anonymous$$onoBehaviour
{
public float Damage = 10.0f;
public bool Stat$$anonymous$$echanicalExpert = false;
void Start()
{
//load stats, example with PlayerPrefs
Stat$$anonymous$$echanicalExpert = (PlayerPrefs.GetInt("SomeUserName.$$anonymous$$echanicalExpert") == 1);
}
void Update()
{
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
//pos/rot is based on the weapon barrels exit point
GameObject bullet = (GameObject)Instantiate(bulletPrefab, position, rotation);
Bullet b = bullet.AddComponent();
foat damage = Damage;
if(Stat$$anonymous$$echanicalExpert)
{
damage += (Damage * 0.1f);
}
b.Damage = damage;
//here you could also set things like starting speed for the bullet and such depending on the weapon type again
}
}
}
Yeh that looks great and the logic of doing it itself will help a lot, some small tweaks and it should work perfectly how I want it
Thanks for the help
Your answer
![](https://koobas.hobune.stream/wayback/20220613195346im_/https://answers.unity.com/themes/thub/images/avi.jpg)