I have a class with my basic enemy AI in it and I have created several AIs that inherit from that one. What I would like to be able to do is create a new prefab throw my script that uses the class on it and then drag my desired AI to the variable in that script. However I cannot find a way to do this. I have done some Google searches with no luck.
1 Answer
1Sounds like what you’re looking for are Interfaces. Have a look at the documentation.
Essentially, you’ll want all of your AI scripts inheriting from an IArtificialIntelligence (I wrote it out cause IAI looks dumb, lol) that enforces methods and properties shared on all AI scripts. Then you can simply set an IArtificialIntelligence AIscript; as a class member and shoud be able to drag and drop anything inheriting from it into that box. ![]()
Spend some time to understand Interfaces. They’re wildly useful and amazing, and are seriously underrated.
EDIT: Ok, lame. I just noticed the javascript tag. This won’t help you. In fact, there may not be a way to do what you want in Javascript. So I know that this isn’t the answer you’re looking for, but please hear me out. Javascript is NOT, I repeat NOT a good language for developing large games in. I’m honestly surprised Unity implements it at all. I kind of feel like doing so was a marketing thing to attract more users. I don’t dislike javascript – it’s pretty cool, but not for Unity. It’s fairly similar to C#, so take some time to get to know C# and you’ll find yourself dealing with significantly less bugs. Anyway. Good luck to you!
Thank you for the response. I'm not so uncomfortable with C# actually. When I started this project with a friend I chose JS because I thought it would be a little easier for him to follow if he needed to do anything(he's an art guy). I also chose it because I had a little more experience with JS. I'm going to read up on your proposed solution. I may switch to C# for future projects. I have another solution in mind but I want to see if any of you guys with more experience can offer a different, better solution.
– nickolouse