Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 eaj · Mar 16, 2013 at 02:42 PM · gameobjectinstanceclasses

How to access an instance of a class when the class isn't known

Hello,

I have a script called Main that does stuff when you make a mouse click. I have a script called Player and another called Soldier. The Soldier script defines a Soldier class and inherits from Player. Player has a function called playerSelected().

I want to query this function and run other functions from the Main script, but I don't know whether I'm calling Player or Soldier, or even GameObject. I tried GameObject to no avail.

Here's the update function of the Main script. It fails at selectedObject.playerSelected(false); because I'm passing it a GameObject when it should be getting a Player object or a Soldier object. How do I determine what kind of object is being passed. Am I going about this the wrong way entirely?

 function Update () {
 
     if (Input.GetMouseButtonDown (0)) {
     
         objectName        = common.getObjectNameFromTap();
         selectedObject = GameObject.Find(selectedObjectName);
         
         if (object != null)
         {
             object = GameObject.Find(objectName);
             
             // deselect currently selected object
             if (selectedObject)
                 selectedObject.playerSelected(false);

 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by whebert · Mar 16, 2013 at 03:39 PM

The reason it fails at selectedObject.playerSelected(false) is because GameObjects know nothing about the method playerSelected(). What you need to do, is obtain a reference to your Player script from the GameObject, then proceed from there. Here is an example:

 selectedObject = GameObject.Find(selectedObjectName);
 
 // Test to make sure you got something back, not null
 if(selectedObject)
 {
    // Now, see if the selectedObject has a Player script attached
    var player : Player = selectedObject.GetComponent(Player);
    if(player)
    {
       // The selectedOject IS a Player, do Player stuff with it.
       // This will work for either a Player, or Soldier, since Soldier inherits from Player
 
       player.playerSelected(false);
 
       // If you want to see if the player is also a Soldier to do Soldier specific stuff
 
       var soldier : Soldier = player as Soldier;
       if(soldier)
       {
          // player IS also a Soldier
       }
       else
       {
          // player is NOT a Soldier
       }
    }
    else
    {
       // The selectedObject is NOT a Player
    }
 
 }
Comment
Add comment · Show 1 · 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
avatar image eaj · Mar 16, 2013 at 11:45 PM 0
Share

Yes, I understand now. Thanks!

avatar image
0

Answer by nsxdavid · Mar 16, 2013 at 03:41 PM

Unity is a component-based architecture. Once you get this basic concept in your head, the rest is easy:

The GameObject is just a container of components. Some components are built-in, like Transform, and the rest come from scripts (when inherited from MonoBehavior). So in your case, the Player script defines a Player component.

When you add the Player component to the GameObject, the GameObject does not suddenly "become" a Player. It's still just a container of components where one of them happens to be Player.

Therefore, when you want to access a function on something, you have to get it's container (the GameObject like you did above) and look inside for the component that has the function (or method in C#) you want to call.

So in JavaScript:

   var player:Player = selectedObject.GetComponent(Player);
   player.playerSelected(false);

In C#

   Player player = selectedObject.GetComponent<Player>();
   player.playerSelected(false);

You can find discussion of access components here.

And remember, any GameObject an have many different components. It is best to structure your game code into individual components that have a very defined function (i.e. try not to dump everything into one class) and have them work with each other. But also keep in mind that a component might not be there! So check for null if there is any doubt.

Comment
Add comment · Show 1 · 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
avatar image eaj · Mar 16, 2013 at 11:46 PM 0
Share

Thanks for the assist! I'm still getting my head around the modularity of unity.

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to add items (or players) to the scene from classes? 1 Answer

Add an instance of a script as a component? 1 Answer

Why does my class script not recognize variables in its own instance? 1 Answer

Fade 2 out of 3 gameobjects on the screen who have the same material 2 Answers

Object Instance Error Message Confusion 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