Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Ultramyth · Jan 01, 2014 at 11:16 AM · c#boolean

Help with boolean updating on all instances of script

I am trying to create a hex grid map where the player can hit x to explore the current hex and the boolean for that hex changes to true. I have had no luck referencing individual instances from the character controller script, so now and trying my luck at the explore function of the hex script on my instanced prefab on the hex grid.

However, when I press x at runtime, all the hexes with the script update, rather than the one that the character controller is currently colliding with.

I have a feeling I am doing something stupid here and would appreciate some direction.

Code: void Update () { if (Input.GetButtonDown ("Explore")) { ExploreHex ();

         }
     }
 
     private void ExploreHex()
     {
         //Determine time passed from exploring
         
         if (Explored ==false)
         {
             
             //timePassed = timePassed + 24;
             
             //Calculate time passed and period of day    
             //daysPassed = (timePassed / 24);
             //hoursPassed = timePassed - (daysPassed * 24);
             //whatPeriodofDay ();
             
             //Set hex to Explored
             Explored = true;
             Debug.Log ("You explore the Hex");
         }
         else 
         {
             Debug.Log ("The Hex has already been Explored");
         }
     }

Also, Explored is initialized here:

 public bool Explored;
Comment
Add comment · Show 4
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 HappyMoo · Jan 01, 2014 at 12:32 PM 1
Share

Do you have this code in every Hex?, i.e. in the hex Prefab? Then every hey will turn explored when you press that button.

What you want is that script on your Player... then you either already know over which hex you are currently and call exploreHex on that, or you do a Raycast to find out which Hey you're over etc.

$$anonymous$$ore details please, if that's not enough to get you going.

avatar image Ultramyth · Jan 01, 2014 at 12:56 PM 0
Share

i've already tried putting the script on the player controller, however i have been unable to get it to work. the scene consists of and a number of different scripts and objects; a player controller with a daytime.cs script attached, a hex grid object which has instanced prefabs as children, composing the geometry Of the hex grid, and each of these prefabs has the same script attached (hexdetails), which deter$$anonymous$$e the terrain type and whetheror not the player has explored the hex.

avatar image HappyMoo · Jan 01, 2014 at 01:08 PM 0
Share

Alright, but you understand the problem here? You can't have if checks to Input.GetButtonDown ("Explore")) in every hex, because then every hex will react.

Every hex has a collider?

avatar image Ultramyth · Jan 01, 2014 at 01:19 PM 0
Share

So it has to be in the update of the player controller ins$$anonymous$$d? Back to square one... I need to reference a particular instance that the player controller is standing on...

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by HappyMoo · Jan 01, 2014 at 01:22 PM

Try something like this:

  • Remove the check from your HexDetail

  • make the ExploreHex method public

  • Make sure every hex has a collider, not your hexgrid a single one!

  • create a new script to attach to the player


    public class HexExplorer : MonoBehaviour { void Update() { if (Input.GetButtonDown ("Explore")) { RaycastHit hit; if (Physics.Raycast(transform.position+Vector3.up, -Vector3.up, out hit, 100f)) { HexDetail theHex = hit.collider.gameObject.getComponent(); if (theHex != null) theHex.ExploreHex(); } } } }

Comment
Add comment · Show 14 · 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 Ultramyth · Jan 01, 2014 at 01:25 PM 0
Share

Thanks, looks promising. I'll try it as soon as I get home!

avatar image Ultramyth · Jan 01, 2014 at 04:45 PM 0
Share

Nothing is happening. I copied the code and adjusted:

 using UnityEngine;
 using System.Collections;
 
 public class HexExplorer : $$anonymous$$onoBehaviour {
     void Update() {
         if (Input.GetButtonDown ("Explore"))
         {
             RaycastHit hit;
             if (Physics.Raycast(transform.position+Vector3.up, -Vector3.up, out hit, 100f))
             {
                 HexDetails theHex = hit.collider.gameObject.GetComponent<HexDetails>();
                 if (theHex != null) theHex.HexExplored();
             }
         }
     }
 }

Since the script is called HexDetails.cs, and the function in the HexDetails.cs for setting the Explored boolean to true is called HexExplored. I have removed the check from HexDetails and HexExplored is already public in so far as I can see:

 public void HexExplored()
     {
         //Set hex to Explored
         Explored = true;
         Debug.Log ("You explore the Hex");
     }

  • The new script is assigned to First Person Controller, my player controller, along with the Daytime script, which no longer references Input.GetButtonDown("Explore")

  • Each instance of prefabCollider(Clone) has a copy of HexDetails on it and a $$anonymous$$eshCollider with IsTrigger selected.

The scene runs fine, but when I press x (assigned to Input.GetButtonDown("Explore")), nothing happens. I am guessing that the rays aren't hitting anything?

avatar image Ultramyth · Jan 01, 2014 at 04:51 PM 0
Share

Whoops, I just remembered I had edited out ExploreHex() from my code, it's back in as written above, and your code now references it properly.

However, again, nothing happens when I hit X. I don't think any ray is hitting the colliders.

avatar image Ultramyth · Jan 01, 2014 at 05:08 PM 0
Share

Ok, I added a debugging code to your script, which, at run time is updating every frame, even though I have not pressed the x key yet... any clues?

 using UnityEngine;
 using System.Collections;
 
 public class HexExplorer : $$anonymous$$onoBehaviour 
 {
     void Start () 
     {
 
     }
 
     void Update() {
         if (Input.GetButtonDown ("Explore"))
         {
             RaycastHit hit;
             if (Physics.Raycast(transform.position+Vector3.up, -Vector3.up, out hit, 100f))
             {
                 HexDetails theHex = hit.collider.gameObject.GetComponent<HexDetails>();
                 if (theHex != null) theHex.ExploreHex();
             }
 
         }
         else
         {
             Debug.Log("Sorry, I did not hit anything");
         }
     }
 }
avatar image Ultramyth · Jan 01, 2014 at 05:11 PM 0
Share

Sorry, to clarify this post ^

At runtime, I am getting the message "Sorry, I did not hit anything" every frame, despite not even touching the 'x' key that the GetButtonDown("Explore") is attached to... my understanding of this code leads me to believe it should only work when I press the 'x' key...

Show more comments
avatar image
0

Answer by sjmorales · Jan 01, 2014 at 12:29 PM

Update is called on all game objects in the game, not just the one the game object is controlling...

You may want to wrap you code in something like this...

If (being_controlled) { ... }

and set a bool value for that object while you are controlling it to true.

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 Ultramyth · Jan 01, 2014 at 01:08 PM 0
Share

but then how will i deter$$anonymous$$e which object is being controlled? If it is a boolean on the instance, i end up with the same problem that i have now. all of the instanced objects have same name, since they are clones of each other.

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Change a Variable with another script not working (C#) 4 Answers

NullRefenceException: Object reference not set an instance of an object 0 Answers

Creating a weapon script. Need Help / advice. 1 Answer

Calling a C# function from a Javascript 3 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