Using enabled and disabled but want the new Vector to be a random from sprites.

Hi what im trying to do is in the DieAndRespawn() function I want the new vector3 to be a random sprite from the Sprites list. Any help appreciated thanks!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PowerUp : MonoBehaviour
{
    public float fallSpeed = 8.0f;
    private int rand;
    public Sprite[] Sprites;


    void Start()
    {
        rand = Random.Range(0, Sprites.Length);
        GetComponent<SpriteRenderer>().sprite = Sprites[rand];
    }

    void Update()
    {

        transform.Translate(Vector3.down * fallSpeed * Time.deltaTime, Space.World);


    }

    void OnTriggerEnter2D(Collider2D collider)
    {
        if (collider.gameObject.tag == "shredder")
        {
            StartCoroutine(DieAndRespawn());

        }

    }


    IEnumerator DieAndRespawn()
    {
        GetComponent<Renderer>().enabled = false;
        yield return new WaitForSeconds(5f);
        int randNum = Random.Range(-7, 8);
        transform.position = new Vector3(randNum, 4, 0);
        transform.rotation = Quaternion.identity;
        GetComponent<Renderer>().enabled = true;

    }
}

You are calling rand in the start function -once-, so your rand will equal whichever value once throughout the game.

You should instantiate an object from your array of Sprites.

start()
{
GameObject instantiatedSprite;
...
}

some update()
{
rand = Random.Range(0, Sprites.Length);
spritePosition = new Vector3(randNum, 4, 0);
...

instantiatedSprite = Instantiate(Sprite[rand], transform.position, Quaternion.identity);
instantiatedSprite.transform.position = spritePosition;

you have complete control over your instantiatedSprite, from moving to Destroying without killing the orginal objects.

Why not have two seperate die and respawn functions, it seems like you’re making it confusing with this?

        IEnumerator Die()
        {
            Destroy(instantiatedSprite);
            yield return new WaitForSeconds(5f);
            Respawn();
        }

Yeah that’s why im trying to spawn a new sprite with a new random number. I tried your code but it doesn’t work or maybe I am doing it wrong idk?

Yes, you’ll need to put it in the correct places, and if you want to create just one sprite, you would have to do the old ‘runOnce’ bool = true once you’ve created.

start()
{
hasCreated = false;
}

spawn()
{
//spawn stuff
if(!hasCreated)
{
//instantiate stuff
hasCreated = true;
}

die()
{//kill the object
hasCreated = false;
}

Just instantiate that object in a ‘spawn’ function and destroy it in a ‘die’ function, which then goes to the ‘spawn’ function again. The line is literally just creating the random object from your array and positioning it.

instantiatedSprite = Instantiate(Sprite[rand], transform.position, Quaternion.identity);

You’re making instantiatedSprite an instance of Sprite[the value of the rand variable], at it’s transform.position and rotation of the component. Unity help has a lot on this, and it’s very simple, but post back if you get stuck!

I’m tired right now but yes im trying to instantiate an object from my array. I am understanding your methodology but I am still getting errors like Cannot convert Sprite to Gameobject etc.

Thanks for the help I will mess around with it more and try understand better

Ah, you will just need to use Sprite in place of GameObject. Sorry, force of habit.

start()
{
Sprite instantiatedSprite;
...
}

Ty dude, really appreciate it, I will try again tomorrow and give you an update

I found the solution, by a youtube video. Thanks I tried your code but it doesn’t work. Thanks anyways