need help with my balloon spawning script

Updated on June 13, 2017 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
6 on June 13, 2017

hello im currently making a game called no touchy cactus whee your a cactus on a skateboard who has to dodge falling balloons 

 

my problem is this im trying to pick a color for the balloons at spawntime and im getting this eror :

 

Assets/Balloonspawner.cs(36,58): error CS0029: Cannot implicitly convert type `int’ to `UnityEngine.Color’

here is the code :

 

 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Balloonspawner : MonoBehaviour
{
 public GameObject balloonprefab;
 private float timetospawn;
 public int numballoonstospawn = 10;
 public float spawnTime = 2f;
 GameObject balloon;
 public List <Color32> ballooncolors = new List<Color32> ();
 void Awake ()
 {
 }
 void Start ()
 {
 InvokeRepeating ("spawnballoon", 0, spawnTime);
 }
 void spawnballoon ()
 {
 Vector2 spawnPoint = new Vector2 (Random.Range (-10f, 10f), transform.position.y);
 balloon = (GameObject)Instantiate (balloonprefab, spawnPoint, Quaternion.identity);
 balloon.GetComponent<SpriteRenderer> ().color = Random.Range (0, ballooncolors.Count - 1); //this is the problem area
 Destroy (balloon, 5f);
 }
}

i’ve been trying to solve this for about an hour and i have to be somewhere soon obviously ive searched google for more than half an hour prior to making this post 

thanks in advance for your help 

 

  • Liked by
Reply
3 on June 13, 2017

Because the random returns an int and not a color, it should be something more like this:

whatever.color = ballooncolors [Random.Range (0, ballooncolors.Count - 1)];

 

Guru
on June 13, 2017

Reasons I should get back into game development / C# again.

I don’t see these things anymore…

Wise
on June 13, 2017

😛

 

Helpful
on June 13, 2017

thank you that fixed it i don’t know how i didn’t catch that 

Show more replies
  • Liked by
Reply
Cancel
1 on June 13, 2017

Have you tried using a normal Color setup instead of Color32?

Wise
on June 13, 2017

Shouldn’t matter too much.

 

Show more replies
  • Liked by
Reply
Cancel