Hello Guest

Author Topic: Two Whack a Mole tutorial bugs & possible fixes (Unity 4.3)  (Read 4010 times)

Rirath

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Two Whack a Mole tutorial bugs & possible fixes (Unity 4.3)
« on: November 15, 2013, 08:06:07 am »
Hey guys, just picked up 2D Toolkit the other day - enjoying it so far.  Immediately went through the Whack a Mole tutorial and I believe I've found two bugs, and two possible fixes, in Unity 4.3.  These are in both the step by step directions, and the RestOfGame_Complete.unitypackage download.

#1 - in MainGameScript.cs

Code: [Select]
randomMole = (int)Random.Range(0, moles.Count - 1);

while(moles[randomMole].sprite.gameObject.activeSelf)
{
randomMole = (int)Random.Range(0, moles.Count - 1);
}

As is, the code appears to never select the last MoleUnit prefab in your game.  If you have 3, it will select from 2.  If you have 6, it will choose between 5. 
Changing

Code: [Select]
randomMole = (int)Random.Range(0, moles.Count - 1);
to

Code: [Select]
randomMole = (int)Random.Range(0, moles.Count);
appears to solve this issue without introducing any new ones. 
Note that (1, moles.Count) does not seem to work - it will also skip a mole and possibly introduce lockups. (see below)

#2 - Related, also in MainGameScript.cs

Code: [Select]
moleLimit = 3;
Unity will hang after running for a short period of time if the number of (selectable) MoleUnits is less than the moleLimit.  For example, 1 or 2 moles with a limit of 3.  The whole program locks up and needs to be shut down via taskmanager.  Setting the range to (0, moles.Count) and making sure the moleLimit is not greater than the number of MoleUnits you have appears to solve this.  The above range fix is important as without it, even 3 moleUnits with a moleLimit of 3 will crash due to only two being available.  I'm not entirely sure what's causing the lockup, however.

I'm a web developer by trade, very new to Unity and only a hobbyist game developer,  so while I have some idea of what's going on it's only the gist.   ;)  But I didn't see these fixes while looking around, so thought I'd post them here in the hopes someone will find this useful.
« Last Edit: November 15, 2013, 08:16:09 am by Rirath »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Two Whack a Mole tutorial bugs & possible fixes (Unity 4.3)
« Reply #1 on: November 15, 2013, 10:58:45 am »
Cheers, I'll get this fixed and sorted out ASAP.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Two Whack a Mole tutorial bugs & possible fixes (Unity 4.3)
« Reply #2 on: November 16, 2013, 10:22:06 pm »
The crash is caused by the while loop below that. I'll update code shortly.