Normally this wouldn't have been an issue for me since I can easily reference an object and its scripts and then create a GUI button to manipulate those; however, for this project I need the GUI to interact with multiple objects dynamically based on which characters are in-use. This is something I have no experience in doing, so I decided to go through some old scripts and Frankenstein something together.
In the end, the solution was far simpler than I thought; over-complication can be a huge downfall. The basic principle of what I did was the same as I used in the Attack/Damage scripts. I told the GUI that if a specific bool was active, then to search for a character object, and to then reference the PerformsAttack script within that object. I then told the GUI that if that bool was active to display a button, and if the button was pressed to run a method.
To have the GUI find specific object, I simply told it to "Find.GameObject("Object"), and to reference it as variable "c" (for current). If the Find.GameObject returns false, then to do nothing, but if it returns to true to then search that object for a script. This was easily accomplished using the line "c.GetComponent <PerformsAttack> ();", and again telling it that if PerformsAttack returned false, then to do nothing. Inside of the PerformsAttack script I created a method using the "public void SpecialAttack ()" base so that it could be referenced from an outside source. Then it was simply a matter of telling the GUI that if both the previous statements returned "!= null" then to display a button called "Special Attack", and if that button were to be pressed to tell the PerformsAttack script to run "PerformsAttack.SpecialAttack ();".
I also needed to know that it was going to work from within game-mode in Unity, so I went ahead and added a Debug.Log line inside the PerformsAttack.SpecialAttack () method simply stating "Special Attack Successful". This was the easiest way I could think of to make sure the logic behind this idea was working properly. Sure enough when the game ran, I clicked the Warrior button, and a button stating "Special Attack" showed up. Upon pressing the button I was greeted with the Debug message "Special Attack Successful" in the console. Perfect!
No comments:
Post a Comment