Home > Software design >  Unity can I instantiate a game object from a scriptable object? - Solved
Unity can I instantiate a game object from a scriptable object? - Solved

Time:02-17

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

Solution: My ShootingArrows function did not have a reference to the WeaponConfig...

    public void ShootingArrows(WeaponConfig weapon){
    ...
            Projectile arrow = Instantiate (
                                    weapon.projectile,
                                    firePoint.position   (Vector3)( 
                                    direction * 0.5f),
                                    firePoint.rotation);
    }

CodePudding user response:

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);
  • Related