Problems with GameObjects in script

Hey, I’ve been working on this random item spawning script for a bit, but as you can probably tell I have no idea what I’m doing :neutral:

using UnityEngine;
using System.Collections;


namespace AssemblyCSharp
{
	public class risHouseFood : MonoBehaviour //Random Item Spawn - House Food
	{
		
	    public GameObject item;
		
		public GameObject CanofBeans ; //Declaring Objects
		public GameObject BoxofStuff;
		
		public int TotalProbability; 
		public int x;
		
		public GameObject[] housefoods = new GameObject[2]; 
		
	
		void Awake()
		{			
		housefoods[0] = CanofBeans; //Set possible items
		housefoods[1] = BoxofStuff;
		TotalProbability = 0;
			
		for (int i = 0; i < housefoods.Length; i++) //Item rarity = Relative probability
			{
				item = housefoods[i];
				TotalProbability += item.rarity;
			}
			
			x = Random.Range (0,TotalProbability); //Generate random value
			
	    for (int i = 0; i < housefoods.Length; i++) //Subtract "rarity" from "x" to determine a random item
			{
				item = housefoods[i];
				x -= item.rarity;
				
				if (x <1)
				{
					Instantiate(item,Vector3(0,0,0),Quaternion.identity); //Spawn item
						return;
						
		        }
			
			
			
			
		}
	}
}

}

Every time I fix and error another takes it’s place. Right now I’ve got 5 critical errors, most of which have something to do with my confused use of “GameObject”, but there’s some other little syntax errors in there too. The “CanofBeans” and “BoxofStuff” are supposed to refer to prefabs of the same name, each of which has a script that also has the same name that declares the ‘rarity’ variable.

Somebody fix me up? lol

Edit: I now know that I’m not referencing the prefabs correctly, still fixing that now.

I’m working with Peasant and any help is appreciated.

I would propose that you let your Item classes inherit from a common baseitem class that would help you find the rarity variable for every item.
To get a reference to that items rarity use
something like itemPrefab.GetComponent().rarity.

If you get that right you also need to rethink your item spawn method, as to me this doesnt make sense.
Instead use a List which you add your items to as often as ‘rarity’ and then use Random.Range(0, listLength)

So the higher the rarity value the higher the chance of spawning…you should rename the varaible maybe :stuck_out_tongue:

I can tell you that Random.Range returns a float not an int, you need to cast to an int or you need to change the type of your variable x into a float.

//snip
  for (int i = 0; i < housefoods.Length; i++) //Item rarity = Relative probability
  {
    item = housefoods[i];
    TotalProbability += item.rarity;
  }

  x = (int)Random.Range (0,TotalProbability); //Generate random value

  for (int i = 0; i < housefoods.Length; i++) //Subtract "rarity" from "x" to determine a random item
  {
    item = housefoods[i];
    x = (int)Random.Range (0,TotalProbability);
    x -= item.rarity;
// end snipe

I could also tell you Random.Range returns an int or a float without casting.

Thanks for the tips. The logic behind the spawn system is a little abstract, but it works just the same as your suggestion. Here’s the idea I was working off (I really wish this forum had spoilers so posts weren’t so massive)

Lol fair point, it still seems intuitive enough for me though. What else could I name it, “commonality”? :roll: