Hello i have almost finished my 2D mobile game but i have a very last issue stepping in my way that i can’t figure out the solution so i’m hoping that someone could help me.
Here is the code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class collectDimonds : MonoBehaviour {
public static int dimonds = 0;
public int dimondValue = 1;
public Transform[] dimondSpawn;
public Image dimond;
public bool spawning = false;
// Use this for initialization
void Start () {
dimonds = PlayerPrefs.GetInt ("Dimanti");
}
void OnTriggerEnter2D(Collider2D coll)
{
if (coll.gameObject.tag == "Dimond")
{
dimonds += dimondValue;
Destroy(coll.gameObject);
spawning = false;
}
}
void Update()
{
PlayerPrefs.SetInt("Dimanti", dimonds);
if (!spawning) {
Spawn();
}
}
void Spawn ()
{
spawning = true;
Transform coinSpawns = dimondSpawn [Random.Range (0, dimondSpawn.Length)];
Image coinPrefab = (Image)Instantiate (dimond, coinSpawns.position, coinSpawns.rotation);
coinPrefab.transform.SetParent(GameObject.FindObjectOfType<Canvas>().gameObject.transform);
}
}
And the error says:
IndexOutOfRangeException: Array index is out of range.
collectDimonds.Spawn() (at Assets/scripts/collectDimonds.cs :54)