- Home /
Change a Class in the inspector
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.
Answer by jtsmith1287 · Sep 11, 2014 at 09:17 PM
Sounds 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 $$anonymous$$d but I want to see if any of you guys with more experience can offer a different, better solution.
Your answer
Follow this Question
Related Questions
Multiple classes inside each other 1 Answer
Is it possible to create a script dinamic like animation>size controling the Elements variables? 1 Answer
JS class inheritance woes. i.e. wtf? 2 Answers
Static array with custom class? 1 Answer
How do I show a Classes Vars in the inspector (Javascript)?? 2 Answers