- Home /
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;
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.
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.
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?
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...
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(); } } } }
Thanks, looks promising. I'll try it as soon as I get home!
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?
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.
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");
}
}
}
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...
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.
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.