Switch shooting balls of cannon

I am making 2d game of cannon shooting.I take fruit item as a bullet.I want to put a option on my game screen by which a GUI with different fruit will be displayed and i can choose any of them as bullet.kindly help me…
This is my current script…

using UnityEngine;
using System.Collections;

public class TurretController : MonoBehaviour {

    private Vector3 mouse_pos;
    private Vector3 object_pos;
    private float angle;
    private float bulletSpeed = 450;
    public GameObject[] ammo;        // Array of enemy prefabs.

    // Use this for initialization
    void Start () {
    }

    void Update(){
    }

    void FixedUpdate () {   
        // Point the cannon at the mouse.
        mouse_pos = Input.mousePosition;
        mouse_pos.z = 0.0f;
        //object_pos = Camera.main.WorldToScreenPoint(transform.position);
        mouse_pos.x = mouse_pos.x - object_pos.x;
        mouse_pos.y = mouse_pos.y - object_pos.y;
        angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg - 20;
        Vector3 rotationVector = new Vector3 (0, 0, angle);
        transform.rotation = Quaternion.Euler(rotationVector);

        // Fire a bullet.
        if(Input.GetMouseButtonDown(0)){
            int ammoIndex = 0;
            GameObject bullet = (GameObject)Instantiate(ammo[ammoIndex], transform.position, transform.rotation);
            bullet.transform.LookAt(mouse_pos);
            bullet.rigidbody2D.AddForce(bullet.transform.forward * bulletSpeed);
        }
    }
    }

What part do you need help with? Building the whole UI?

Generally, it’s better to ask when you’re having trouble with one particular thing. Like you’re implementing a function and it’s not working the way you expect it to. Asking the forum to build your whole interface and weapon selection system is just short of asking us to make your game for you.

I recommend you check out the UI tutorials. Go through them and take a stab at doing it yourself. If there’s something specific you can’t figure out, feel free to ask.

I already have UI. I just want help in scripting to choose the fruit item to use it as a cannon bullet.You can see my UI attached.

You need to map each gui image to a prefab of the actual item so when the player , for example, clicks the apple then the apple prefab is instantiated as the bullet. I’m not sure of the best way but you could have the ammo as an array/list of prefabs & assign an int dependent on what they clicked on then have the instantiate use that
E.g. (Not real code, just as an idea)
Instantiate (ammo[# assigned], transform.position, transform.rotation);

1 Like

Thanks man…I had same idea in my mind but i am getting problem to implement this scenario in my code :frowning: Kindly help me in code

Sorry, I don’t use gui items like that so I have no code handy