First, let’s just have a couple seconds to get our heads around that needless spoonerism of a title… okay? good.
So then the dev-blog, well if anyone has read the “Goals“ post, they’ll know roughly where I am, and if you’re read part of my portfolio you might be a little confused so let’s set everything out here and now.
For those who haven’t read the “Goals” post, I recently made a promise to re-create several classical games with a total of 8 games to choose from, with the first one already chosen – Pong… again. Some might question, “If you’ve made Pong once, then why on earth are you making it again?“.
“But the first game still worked though, why go back?“, well I know a lot of gamedevs get really worried about having learnt something new and then go back and re-vamp older parts of the project so they’re up to snuff, and this can become a hazardous spiral to fall into. This isn’t that, I’m very proud that the game is ‘better’, even if it is only known to me, but this project is more than anything warming up my C# skills for the new challenges of the other classic games.
This time I have the game manager have a function called “SetScene” to create the ball (which is invoked after a goal is scored to give a brief game pause before a new round), and from there it would call a function in the AI script to set the scripted ball object to the object called once again “Ball(Clone)“, but it wasn’t working… at all. This was strange as I even started to invoke the AI’s function a half second later so that it would hopefully find the ball, because maybe I was just asking too quickly? This answer barely worked, and when I say barely I mean maybe 1 in 5 balls would instantiate quick enough for it to work.
So what’s to be done? You need the AI to near instantly find the ball so that it can return a serve, otherwise it’s not a fair game and you’re just watching a ball fall into the opponent’s goal, it isn’t a matter of challenge at all. As it happens I found the answer courtesy of an outside of the box thinker on the Unity answer forums. They suggested setting the game object variable to actually equal the instantiate method, there was a minor issue of casting an object into a game object but it worked. Now the AI can instantly find the ball and act as a challenging opponent.
A somewhat simple solution perhaps, but I’d never thought of setting a variable as being equal to an instantiation, and it’s just one of those things that you’re never really aware of until it’s pointed out to you in such an obvious manner that you’ll never forget it. So if you’re having a similar issue perhaps this post might help you find that special gameobject out there! Until next time keep designing, have an awesome day, and thanks for reading!