Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by matt_1 · Jul 05, 2016 at 03:57 PM · listname

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

60 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

find all objects with a particular name that have been scaled 1 Answer

Overriding App icon string for Android builds 1 Answer

How to get position of an object in a list 2 Answers

Error while debugging list 1 Answer

Created a card hand from a list but cannot access card hand 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges