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;
}
}
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.
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.
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