Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zrol84 · Mar 31, 2017 at 02:40 PM · unity 5gameobjectfunctionparametersfinite-state-machine

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges