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

0

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

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?
That sounds like you didn't reference your Bow asset into the config field then. Otherwise Instantiate(null) would throw a different exception ;)
Yep! Finally found it, I never added a reference to my WeaponConfig in my ShootingArrows function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.