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 /
This question was closed Dec 20, 2016 at 06:13 PM by barbasu for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by barbasu · Dec 20, 2016 at 03:24 PM · c#unity 5

Mining script

I am figuring out a mining script so that player can mine rocks and what not. It somewhat works but not as I would like it to. Basically it works for the first rock but any other rocks i place are not working at all. For example if i place just 1 rock it works fine. If i add 3 more rocks only the 4th one works and the first three don't.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MiningRocks : MonoBehaviour
 {
 
     public int distancetomine = 2;
     bool rockInRange;
     GameObject rock;
     IronOre ironore;
 
 
     // Use this for initialization
     void Awake()
     {
        rock = GameObject.FindGameObjectWithTag("IronOre");
        ironore = rock.GetComponent<IronOre>();
 
     }
 
 
     // Update is called once per frame
     void Update()
     {
         float distance = Vector3.Distance(this.transform.position, GameObject.FindGameObjectWithTag("IronOre").transform.position);
         if (Input.GetKeyDown(KeyCode.E) && distance <= distancetomine && rockInRange == true)
         {
             ironore.IronOreHealth -= 1;
         }
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject == rock)
         {
             rockInRange = true;
         }
     }
 
     void OnTriggerExit(Collider other)
     {
         if (other.gameObject == rock)
         {
             rockInRange = false;
         }
     }
 
     void OnGUI()
    {
             float distance = Vector3.Distance(this.transform.position, GameObject.FindGameObjectWithTag("IronOre").transform.position); ;
             if (distance <= 2)
             {
                 GUI.Box(new Rect(400, 200, 220, 22), "Press E to gather");
             }
     }
 
 }

Script is attached to the player. I would appreciate any help on what i am doing wrong.

PS (if i place 3 rocks) the GUI only shows for the 3rd rock placed. If i mine that rock it then shows to for 2nd and then for the first while it should show for all at all times + if i try to mine 1st or 2nd rock it doesnt work. It only works if i go 3rd>2nd>1st.

Comment
Add comment · Show 3
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
avatar image Landern · Dec 20, 2016 at 03:59 PM 0
Share

So you're using an old version of unity? FindGameObjectWithTag was replaced with FindWithTag and for plural(more then one, an array is returned) FindGameObjectsWithTag. With that said, if you had multiple tagged game objects called "IronOre" in the scene and you attached this script to the player, wouldn't that mean that Awake(which sets the reference to the Ore gameobject) would only execute once? Wouldn't that also mean that it would return a single GameObject in whatever order Unity seeks and finds, perhaps only returning the first found and nothing else? If you created more IronOre gameobjects during the scenes play Awake wouldn't be called again. Things to consider.

avatar image barbasu Landern · Dec 20, 2016 at 04:23 PM 0
Share

very true I have change the script a bit and put it on the rocks ins$$anonymous$$d of player. Works fine now.

avatar image oswin_c barbasu · Dec 20, 2016 at 04:28 PM 0
Share

Do be careful! IF you are using Update() on every consecutive rock, you will find your game will slow down substantially when you are dealing with a large area.

The fewer update functions you call, the less you have to compute between frames. If you can do all of the work without an update function, all the better.

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by oswin_c · Dec 20, 2016 at 04:09 PM

On awake, you call FindGameObjectWithTag("IronOre"), which gives you the first rock that monobehavior finds with that tag.

Then, when finding the distance, you call the function again in calling the distance, in Update() and in onGUI.

Since FindGameObjecTWithTag will only return the first object found, it is no surprise that you're getting the distance to the third rock.

When you call OnTriggerEnter, you check if the game object is that rock, i.e the first one that monobehavior found. That condition will fail for every other rock.

I think what you are looking for is almost identical in code, but plural.

You want:

 GameObject[] rocks;
 
 void Awake {
     rocks = (GameObject) FindGameObjectsWithTag("IronOre");
 }

This will return not just the first object with that tag, but literally all of them. Note that GameObject[] is an array. Also, FindGameObjectsWithTag gives an array of Object[], not GameObject[], so I use a simple and shoddy cast (GameObject).

However, given that you are actually looking for the IronOre component, FindObjectsOfType is better.

 IronOre[] rocks;
 
 void Awake()
 {
     rocks = (GameObject) FindObjectsOfType<IronOre>();
 }

In Update, you will want to check the distance to all rocks, not just one.

 void Update()
 {
 ...blah blah blah
     for (int i=0; i<rocks.Length; i++)
     {
         float distance = Vector3.Distance(transform.position, rocks[i].transform.position);
         if (input.GetKey.... blah blah blah)
         {
                 rocks[i].IronOreHealth = -1;
         }
      }
 }


You could loop through OnTriggerEnter and check every objet against every one in your rocks array, but, there's a much easier way of doing it.

 void OnTriggerEnter(Collider other)
 {
      if (other.gameObject.GetComponent<IronOre>() != null) rockInRange = true;
 }

Likewise for OnTriggerExit.

Good luck!

Comment
Add comment · Show 1 · Share
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
avatar image barbasu · Dec 20, 2016 at 04:21 PM 0
Share

Thank you for this long and explanatory answer, I have however found a solution, which happened to be 2 mistakes in the code. It seems to be working fine now no matter how many new rocks i place and it works for all of them at the same time. I will close this and pick this as an answer as i think it would work with your solution as well.

avatar image
0

Answer by Glurth · Dec 20, 2016 at 03:54 PM

Just a guess, but perhaps the issue arises because you are using GetKeyDown. From it's docs:"...since the state gets reset each frame. It will not return true until the user has released the key and pressed it"

As opposed to GetKey, which will always return true when the key is held down. https://docs.unity3d.com/ScriptReference/Input.GetKey.html

Comment
Add comment · Show 1 · Share
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
avatar image barbasu · Dec 20, 2016 at 04:05 PM 0
Share

still works the same

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

IndexOutOfRangeExeption! This doesent even affect my gameplay, it just is there to be annoying! 1 Answer

NullReferenceException: Error from the GetStyle() - Method 1 Answer

Rayscast from the GameObject that is not the player does not work 0 Answers

UnityAction Vs UnityEvent 1 Answer

Unity for each JSONnode in JSONObject 0 Answers


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