Getting list to print correctly and collider raycast to not get an error when i read first object to second object
So i have my list, but it doesnt update it consistently, if i put in "apples", then "fruit2", the order of the list when i print it should be apples, then fruits. but all i get is whatever the first item i put in. The list does read the amount i put in. if i put 2 items in, it prints out 2 items, but only the one that i put in first. What i know is that, if i click on apples, it reads blueberries, and if i click olives, hoping that it will break, it reads apples. Thanks. below are the two classes that work with each other.
using UnityEngine;
using System.Collections;
public class spawnFruit : MonoBehaviour {
int spawnNum = 15;
void spawn()
{
for(int i = 0; i<spawnNum; i++)
{
int rand = Random.Range(0, 3);
GameObject fruit = GameObject.CreatePrimitive(PrimitiveType.Sphere);
if (rand == 0)
{
fruit.GetComponent<Renderer>().material.color = Color.red;
fruit.name = "apples";
print("Created apples");
}
if (rand == 1)
{
fruit.GetComponent<Renderer>().material.color = Color.blue;
fruit.name = "blueberries";
print("Created blueberries");
}
if (rand == 2)
{
fruit.GetComponent<Renderer>().material.color = Color.green;
fruit.name = "olives";
print("Created olives");
}
fruit.transform.localScale =new Vector3(0.1f, 0.1f, 0.1f);
fruit.transform.position = new Vector3(this.transform.position.x + Random.Range(-1.0f, 1.0f),
this.transform.position.y + Random.Range(0.0f, 2.0f),
this.transform.position.z + Random.Range(-1.0f, 1.0f));
}
}
// Use this for initialization
void Start () {
spawn();
}
// Update is called once per frame
void Update () {
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class PickUP : MonoBehaviour {
public int distanceToItem;
public Slider food;
public Slider water;
private float foodcounter;
List<GameObject> inventory;
public Player_stats playerstats;
public ArrayList pickupobjects;
// Use this for initialization
void Start () {
inventory = new List<GameObject>();
}
void Collect()
{
if (Input.GetMouseButtonUp(1))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out hit, distanceToItem))
{
GameObject fruity = (GameObject)FindObjectOfType(typeof(GameObject));
if (hit.collider.gameObject.name == "apples")
{
Debug.Log("item hit");
if (fruity)
{
Debug.Log("Gameobject found: " + fruity.name);
}
foreach (var go in inventory)
{
Debug.Log("current inventory: " + go.name);
}
foodcounter = playerstats.foodcount;
foodcounter += 10;
if (foodcounter >= 100)
{
foodcounter = 100;
}
playerstats.foodcount = foodcounter;
playerstats.food.value = playerstats.foodcount;
print("food value is " + foodcounter);
}
if(hit.collider.gameObject.name == "blueberries")
{
Debug.Log("item hit");
//GameObject fruity = (GameObject)FindObjectOfType(typeof(GameObject));
if (fruity)
{
Debug.Log("Gameobject found: " + fruity.name);
}
foreach (var go in inventory)
{
Debug.Log("current inventory: " + go.name);
}
foodcounter = playerstats.foodcount;
foodcounter += 10;
if (foodcounter >= 100)
{
foodcounter = 100;
}
playerstats.foodcount = foodcounter;
playerstats.food.value = playerstats.foodcount;
print("food value is " + foodcounter);
}
if(hit.collider.gameObject.name == "olives")
{
Debug.Log("item hit");
//GameObject fruity = (GameObject)FindObjectOfType(typeof(GameObject));
if (fruity)
{
Debug.Log("Gameobject found: " + fruity.name);
}
foreach (var go in inventory)
{
Debug.Log("current inventory: " + go.name);
}
foodcounter = playerstats.foodcount;
foodcounter += 10;
if (foodcounter >= 100)
{
foodcounter = 100;
}
playerstats.foodcount = foodcounter;
playerstats.food.value = playerstats.foodcount;
print("food value is " + foodcounter);
}
// inventory.Add(fruity.gameObject);
Destroy(hit.collider.gameObject);
}
}
}
// Update is called once per frame
void Update () {
Collect();
}
} '
Comment
Your answer