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 zpinner · Feb 20, 2012 at 01:25 PM · javascriptsyntaxtypecast

how to typecast from GameObject

Does UnityScript typecasts objects? how?

 function ItemHandler(item : GameObject) {
     if(item instanceof EquippableItem) {
        DoSomethingWithEquippableItem(itemtypecasted);
     }
 }
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
3

Answer by Aeless · Feb 20, 2012 at 01:45 PM

for that kind of behavior you're actually adding components to game objects. you don't derive from game object. if you want to check whether a game object has a specific component, use something like:

 if (GetComponent<EquippableItem>() != null)
 {
 }

but just a reminder, in unity, you don't actually need to know the type of the object/component or anything actually. just send a message whenever you want something to happen. then handle that event in the class itself. in your case:

 class EquippableItem : MonoBehaviour
 {
    void OnMyEvent()
    {
      DoSomethingHere();
    }
 }

then whenever you want to trigger that just call

 targetGameObject.SendMessage("OnMyEvent")

for the game object we're talking about.

take a look at these methods from component:

http://unity3d.com/support/documentation/ScriptReference/Component.SendMessage.html http://unity3d.com/support/documentation/ScriptReference/Component.BroadcastMessage.html http://unity3d.com/support/documentation/ScriptReference/Component.SendMessageUpwards.html

also for accessing components:

http://unity3d.com/support/documentation/ScriptReference/Component.GetComponent.html http://unity3d.com/support/documentation/ScriptReference/Component.GetComponentInChildren.html

Comment
Add comment · Show 2 · 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 zpinner · Feb 20, 2012 at 02:07 PM 0
Share

well, instanceof works. so I guessed that if instanceof works, why wouldn't a typecast?

avatar image zpinner · Feb 20, 2012 at 02:19 PM 0
Share

well, it seems that instanceof doesn't work, mono has tricked me, sorry.

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

6 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Is it possible to declare a null or escaped char in Unityscript 2 Answers

Quick GetComponent syntax problem! 2 Answers

How to restrict a button for a second 1 Answer

why doesent my javascript unity code work 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