- Home /
How to make a game object of a certain class?
Hi guys,
I couldn't find this anywhere so I thought I'd just ask. I'm a sort of comfortable with Unity, but I'm no master. The thing I have been trying to do is make a Finite State Machine AI. I wish to use the enemy game object as a parameter in some of the functions I am using. The script that contains the class that contains SOME these functions is attached to the one enemy I currently have in the game. I also need to use the enemy as a parameter in functions from other scripts attached to other game object. Let's say my function is
MyFunc(Enemy enemy)
So I thought I would do something like
MyFunc(this)
but "this" seems to refer to the class and not the game object. I tried using
MyFunc(gameObject)
because gameObject refers to the object holding the script, but gameObject is a game object and not of class Enemy. So the thing I am really asking here is how do I get the enemy object in the game to be of class Enemy and use it as a parameter in these functions. Also, I know this might be either impossible or unfavorable so I am definitely not opposed to suggestions on different ways to do this.
Thanks
Answer by Commoble · Mar 31, 2017 at 05:39 PM
If you have two scripts on an object and you want one script to access the fields and functions on the other script, you need the one script to get a reference to the other script.
There's two ways to do this:
1) In your script, define:
public Enemy enemyScript;
This will create a field in the inspector window for your script while inspecting your gameobject, which you can drag your Enemy script to; Unity will assign the Enemy component to this field when the scene initializes.
2) Alternatively, you can use
this.enemyScript = this.gameObject.GetComponent<Enemy>();
Which gets a reference to the Enemy component on the same GameObject. If you do this, I suggest A) getting the reference in the Start event of your script so that it's only done once, and B) not making your enemyScript field public (to reduce inspector clutter).
You can read more about this subject here and here. GetComponent's documentation can be found here.
With regards to whether the practice of having scripts call functions on other scripts is favorable: It's perfectly fine, and it's generally a good practice not to put too much power in a single component. Just don't let the references between scripts get too convoluted; I suggest avoiding having two scripts both have references to each other, and avoiding having a script A get a reference to a script B by asking script C for it. Additionally, use Awake to initialize as much of a script's data as you can without referencing other objects or components, and then use Start to get references to other scripts and objects and finish initializing.
Your answer
Follow this Question
Related Questions
Function to assign gameobject components with parameter strings... 1 Answer
C# UI component overflaping another gameobject function 0 Answers
Message sender...how to know who send the message 1 Answer
Can You Convert a Transform into an Image? 2 Answers
Creating a GameObject variable without instantiating it? 1 Answer