0

I want each bow to shoot a different arrow that I set in the SO when it is created. I need each bow to shoot the assigned arrow prefab in the SO.

WeaponConfig has [SerializeField] public GameObject projectile; field where I want to be able to swap out different arrows so each new weapon can shoot different arrows that I will set in the inspector.

Shoot Script:

    public void ShootingArrows()
    {
        var screenCam = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        Vector3 worldMousePos = screenCam;

        // Direction of mouse to player
        Vector2 direction = (Vector2)((worldMousePos - firePoint.position));
        direction.Normalize();

        // Creates the arrow locally
        GameObject arrow = (GameObject)Instantiate (
                                arrowPrefab,
                                firePoint.position + (Vector3)( direction * 0.5f),
                                firePoint.rotation);

        FindObjectOfType<AudioManager>().Play("BowShoot");
        
        // Adds velocity to the arrow
        arrow.GetComponent<Rigidbody2D>().velocity = direction * arrowForce;

        // Rotate arrow based on position from player to cursor
        angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - 90f;
        arrow.transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.AngleAxis(angle, Vector3.forward), 1000 * Time.deltaTime);
    }   

In my Instantiate I have arrowPrefab called that is set as a game object at the top of the script.

What do I change this to, to instantiate the intended projectile?

If I change it to this code below then it is saying it does not have an object reference.

     GameObject arrow = (GameObject)Instantiate (
                             WeaponConfig.projectile,
                             firePoint.position + (Vector3)( direction * 0.5f),
                             firePoint.rotation);

I have the Projectile slot in the inspector filled in with the intended arrow I want shot. Each one has a different prefab selected. How do I instantiate the assigned arrow in the SO?

enter image description here

Nimantha
  • 6,405
  • 6
  • 28
  • 69
David
  • 705
  • 1
  • 6
  • 22

1 Answers1

1

Well instead of the so far

public GameObject arrowPrefab;

you would need to have a

public WeaponConfig config:

field, reference your Bow asset there and then do

GameObject arrow = Instantiate (
                         config.projectile,
                         firePoint.position + (Vector3)(direction * 0.5f),
                         firePoint.rotation);
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • That is what I had earlier but the config.projectile then complains about Object reference not set to an instance of an object. How do I assign the projectile to the instance? – David Feb 15 '22 at 19:04
  • 1
    That sounds like you didn't reference your `Bow` asset into the `config` field then. Otherwise `Instantiate(null)` would throw a different exception ;) – derHugo Feb 15 '22 at 19:54
  • Yep! Finally found it, I never added a reference to my WeaponConfig in my ShootingArrows function. – David Feb 16 '22 at 16:02