I am working on a match 3 game and I have a case where I am creating a new “bomb” piece and trying to set the bool “isRowBomb” true. Unfortunately my code isn’t doing that. I am able to set other bools on the new piece but not the one I need to use.
piece.GetComponent<Dot>().isRowBomb = true; // not working
piece.GetComponent<Dot>().isNewBomb = true; // works fine
Debug.Log("value: " + piece.GetComponent<Dot>().isRowBomb);
// Debug says true but it doesn't work as a row bomb when matched
// It also does not show as true in the editor
This is the bare minimum of information to report:
what you want
what you tried
what you expected to happen
what actually happened, log output, variable values, and especially any errors you see - links to documentation you used to cross-check your work (CRITICAL!!!)
If you have no idea what is happening, fix that FIRST. Here’s how:
Time to start debugging! Here is how you can begin your exciting new debugging adventures:
You must find a way to get the information you need in order to reason about what the problem is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
you’re getting an error or warning and you haven’t noticed it in the console window
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the names of the GameObjects or Components involved?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);
If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.
You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.
You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654
If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.
Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
Are you sure? Switch your inspector to “debug” mode so you can see the instance ID, then also use Debug.Log(piece.GetInstanceID()); to see if the one reported in your script are the same as the one in your inspector.
I will grant you that my title was not the most helpful. I should have used something like “Unable to set boolean value on game object through C# script”. I want to thank you for taking the time to respond. I know you are under no obligation to try to help noobs like me.
I understand you get a lot of frustratingly vague posts here and everyone has their limit but I feel that if you had read everything I posted it would have answered many of your questions.
What I want is for the boolean value “isRowBomb” to be set to true.
What I tried: piece.GetComponent<Dot>().isRowBomb = true; When that didn’t set the value to true I added Debug.Log("value: " + piece.GetComponent<Dot>().isRowBomb); which returns “true” however as shown in the screenshot of the inspector and as stated in the comments I included in the code, the value was not actually set true. I then created a new boolean value and tried to set that as true immediately following the “isRowBomb” line piece.GetComponent<Dot>().isNewBomb = true;
As stated in the comments this sets the value of “isNewBomb” to true as expected which shows that I am accessing the game object and component I am expecting the code to affect and that my method of setting a boolean value is working in general.
I did not explicitly state that there were no errors in the console, but there were no errors in the console.
I could have included one other thing I tried which I will do here.
Initially I was instantiating the piece with this code:
GameObject piece = Instantiate(board.dots[2], copyPosition, Quaternion.identity);
/* where board.dots[2] is an item from an array of available game objects
public GameObject[] dots;
*/
After some searching turned up this thread: Bool on an instantiated prefab
I tried changing it to GameObject piece = Instantiate(board.dots[GetDotIndex(this.tag)], copyPosition, Quaternion.identity) as GameObject;
That change did not alter the results. The “isRowBomb” boolean was still not being set True and the “isNewBomb” boolean was.
My code thus far has come from the “Mister Taft Creates” “Make a game like Candy Crush using Unity and C#” video tutorials on YouTube. I know both from other coding experience and from Mister Taft’s own admissions that the way certain things are done in the series are not the best ways to do them and that there are still other issues in the code. This thread is part of my attempt to resolve one of the issues that was not covered in the tutorials. I am hesitant to share the whole code because it is a bit of a mess since some of it evolved and changed as the tutorials went on and he either found or thought of other/better ways to do things.
Please let me know what other information I can provide to help debug this.
In my next reply to kdgalla I will share a screenshot of the inspector in debug mode with the object in question selected in the scene.
*edited to remove attitude. I am sorry for any attitude that remains, I am grateful for all attempts to help me resolve this.
You may be on to something with this. I have switched to debug mode and added your line Debug.Log(piece.GetInstanceID()); and did my tests again and the ID it returned does not match the ID on the game object I am looking at.
I added this line Debug.Log(piece.GetComponent<Dot>().GetInstanceID()); because I saw that each component has its own instance ID and ran the test again. The piece ID does not match any component of the piece but the Dot component ID does match. I am not sure what that means. I have included screenshots of the console and the debug panel.
I am not sure what to make of that or what the next step would be.
I feel so dumb now.
I did a search to make sure there wasn’t code somewhere else changing the value after I tried to set it. I found a “isRowBomb = false;” in the start method of the dot component. I didn’t think that would be the cause because we are past the start point of that script when I am setting the new object but I commented it out to be thorough and because there is a strong chance that I don’t understand what is really happening. And that was the problem.
I am not sure why that line wouldn’t affect other rowBomb pieces created elsewhere in the same script but it doesn’t.
I clearly have a lot to learn.
I thank you for all of your help and I want to apologize again for any attitude I missed when editing my above post to Kurt-Dekker.