Instantiating an object with data (c#)

I want to instantiate a projectile for a weapon that has properties based off the weapon that fired it such as range, damage, spread, etc, etc

this data is stored inside weaponobjects which are scriptable objects, what are the different ways in which I can get the data from the weaponobject to use as variables in the projectile?

The best way, I believe, is to create a Struct that stores all that information, and pass it onto your new projectile:

// Struct that carries all of your information about the projectile fired
struct ProjectileInfo {
	public readonly float Range;
	public readonly float Damage;
	public readonly float Spread;
	// other variables that you want, make a paramter below for each one.

	// You can instantiate this at runtime instead of storing values on a projectile prefab or whatever
	public ProjectileInfo(float range, float dmg, float spread) {
		Range = range;
		Damage = dmg;
		Spread = spread;

public class Projectile : MonoBehaviour {
	public ProjectileInfo Info { get; set; }

	// ... your projectile code ...

public class WeaponObject : MonoBehaviour {
	// ... your weapon code ...

	public float Range;
	public float Damage;
	public float Spread;

	void Fire() {
		projectile = InstantiateProjectile();

		projectile.GetComponent<Projectile>().Info = new ProjectileInfo(Range, Damage, Spread);

	// ... your weapon code ...

You could simply assign the range, damage, spread when the projectile is created. This does mean that you have to create a script for the projectiles to hold the info. But here is a simple idea on how to do it.

Add this to your gun:

using UnityEngine;
using System.Collections;

public class weaponObject : MonoBehaviour {

    //Your info on the gun
    public float range;
    public float damage;
    public float spread;

    public bool fired;//You dont have to use this way of firing your weapon, there tons of better ways to do it.
    public GameObject projectilePrefab; //Place the projectile prefab here from the editor.

    //I would have an empty object in front of the barrel somewhere so the projectile spawns there.
    public Vector3 BarrelsPosition;
	void Update ()
        if (fired == true)//Can replace the way you call the function

    void SpawnProjectile()
        GameObject projectile = (GameObject)Instantiate(projectilePrefab, BarrelsPosition, Quaternion.identity);
        projectile.GetComponent<projectileInfo>().projectileRange = range;
        projectile.GetComponent<projectileInfo>().projectileDamage = damage;
        projectile.GetComponent<projectileInfo>().projectileSpread = spread;

And add this to your projectile and make it a prefab:

using UnityEngine;
using System.Collections;

public class projectileInfo : MonoBehaviour
    public float projectileRange;
    public float projectileDamage;
    public float projectileSpread;

Once the info has been passed to the projectile you can have it do things based on your values in the projectileInfo script if you want. The only problem is that you going to constantly be instantiating object which could cause some garbage collection. i would recommend that you look into object pooling, this way you can create a few bullets and recycle them. This is what i did with my gun:

  • As i instantiate my gun into the world i also instantiate the bullets (about 10-30 depending on the rate of fire) and i just disable the bullets, not then gun.
  • When I fire the gun i enable the bullets based on the pooling system (it keep track of which bullet has fired and which one to fire next) and the bullet then disables itself with a script after it has competed the tasks it should.

This way of doing it will help with the GC, especially if the object has quite a few verts and tris to it. Also i haven’t tested the script but it should work, just let me know if it does and i hope this is what you were looking for :smiley: