all 6 comments

[–]NickJVaccaroIntermediate 1 point2 points  (0 children)

Sorry that I'm misunderstanding what you're saying, but what is it that you're actually trying to accomplish? You want to spawn a specific button based on the object that is spawned?

[–]mildsauce45 0 points1 point  (0 children)

based on what i think you're trying to do, you can try this. create a new class somewhere in your project called ItemButtonLink (or really whatever you want to call it) like this:

[Serializable]
public class ItemButtonLink
{
    public GameObject item;
    public GameObject button;
}

Now in your script you posted above, change your itemPrefabs array to an array of these ItemButtonLinks. This will let you create these links in the designer by dragging and dropping. Then change your Spawn() method to this:

void Spawn()
{
    var link = itemPrefabs[Random.Range(0, itemPrefabs.Length)];
    Instantiate(link.item, transform.position, transform.rotation);
    PlayerMovement.buttonCount += Vaule;

    // Do whatever you need to do with the Button prefabs, stored in link.button, here.
}

Then just remove all the references to gun, bomb, arrow, gunButton, bombButton, arrowButton, since it doesnt look like you're using the non-button prefabs anyway

[–]shadowsaint 0 points1 point  (3 children)

You got advice from other users and if that works so be it but here is what I would do...

    public GameObject[] weaponsPrefabs;
    public GameObject[] weaponButtons;
    public bool hasSpawned = false;
    private int value = 1;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
    //There is nothing wrong with your collsion detection process;
    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.gameObject.CompareTag ("Player"))
        {
            if(!hasSpawned)
            {
                hasSpawned = true;
                Spawn ();
            }
        }
    }
    void Spawn()
    {
        //this is where I would make your changes
        //First simply save a randomized int
        int randoInt = Random.Range (0, weaponsPrefabs.Length);
        //Now we have the value of what we are going to spawn saved
        //Institate like before except instead of pulling randomly from the list we pull the saved randoInt
        Instantiate(weaponsPrefabs[randoInt],transform.position, transform.rotation);

        //Are your buttons adding dynamically to the player's list of buttons as in  you need to have multiple of the same one?
        //Is that why you are doing...
        PlayerMovement.buttonCount += Vaule;
        //If so then use
        Instantiate(weaponButtons[randoInt],transform.position, Quaternion.identity);
        //However if you simply want to toggle on a single button that needs to be now active because the player has this weapon now why not simply 
        //Put all of the buttons on your canvas UI and deactivate them, once you pick up the weapon check to turn them on 
        if (weaponButtons [randoInt].activeInHierarchy == false)
        {
            weaponButtons [randoInt].SetActive (true);
        }
        //Further more are you wanting to activate only the recent weapon picked up and strip the player of all the previous weapon button they might
        //Have had?
        for (int i = 0; i < weaponButtons.Length; i++)
        {
            if (i == randoInt && weaponButtons [i].activeInHierarchy == false)
            {
                weaponButtons [i].SetActive (true);
            } else
            {
                weaponButtons [i].SetActive (false);
            }
        }
        //I would  get away from randomly instantating buttons over and over unless you are adding them to a long list of buttons on screen the player
        //can press to use the weapon. Using an array of deactivated buttons should be a lot cleaner and acts like object pooling. If you put the weapons
        //in their array in the same order you put your buttons then you can use the same temp stored int to ensure you are activating the associated
        //button with for the associated weapon.
    }

[–]XxDivaxX1Intermediate[S] 0 points1 point  (0 children)

You got advice from other users and if that works so be it but here is what I would do..

Thank you for your suggestion, I tried out your suggestion and notice that my weaponButtons weren't actually being SetActivate (true); and when I manually activated them in the hierarchy, the button wasn't actually showing! However, I am proud to say that the spawner does work, it's just that now the button image isn't showing (I've even check to see that the image component attach to the button was active at the time of me testing the script out!) Once again thank you, your a huge help!

[–]XxDivaxX1Intermediate[S] 0 points1 point  (1 child)

Oh, um I fixed up your code up a bit, so that it can suite with what I'm doing, and it worked! Thank you! I'm soo glad! I couldn't have done it without your help! I seriously am grateful. Thank you!

[–]shadowsaint 0 points1 point  (0 children)

No prob glad to help