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 Matthew 5 · Apr 12, 2011 at 06:27 AM · arraymobiletypecallcast

Array of GameObjects Android/iPhone - call/typing/casting problem?

Hi all,

Ok, i've had problems before with typing and casting, and today is no exception.

I've tried using generic casting to make this work on mobile (it works as as on PC, but not on mobile)...

script was;

var MenuItems : GameObject [];

function OnGui () {

//cut some code

if ( GUI.Button ( Rect(ButtonMargin + ((Screen.width - ButtonMargin)/MenuItems.length) MenuIndex ,Screen.height - ButtonHeightOffset - ButtonMargin,(Screen.width - ButtonMargin 2)/MenuItems.length - ButtonMargin,ButtonHeight ), MenuItems[MenuIndex].GetComponent("MenuItem").MenuItemName ) )

//cut some code

}

i got an error - MenuItemName is not a member of UnityEngine.Component..

I change;

MenuItems[MenuIndex].GetComponent("MenuItem").MenuItemName to MenuItems[MenuIndex].GetComponent.<MenuItem>().MenuItemName

but then i get - Unity.MenuItem must derive from UnityEngine.Component in order to substitute generic parameter T in UnityEngine.GameObject.GetComponent()

Like i always say, if this is a simple thing which i know it is, a hand in the right direction instead of the answer is always the best answer, otherwise i still appreciate any help given (answer given, lol!)

Thank you guys again.

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

3 Replies

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

Answer by Extrakun · Apr 12, 2011 at 07:05 AM

Wait, is MenuItem your own Component, or are you referring to the Unity3D MenuItem class?

If you are referring to your own component, first possibility I see is that the namespace is clashing. MenuItem is already used by Unity3D engine, so perhaps you want to use another name for your component.

If you want to refer to the Unity3D's MenuItem class, then you are getting the error because MenuItem is not a component. There are two ways of casting in C#:

MenuItem m = (genericObject) as MenuItem

Or

MenuItem m = (MenuItem)genericObject

More info on how those two methods differ can be found here.

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 Matthew 5 · Apr 12, 2011 at 11:31 PM 0
Share

$$anonymous$$y problem was the fact the namespace was taken. I didnt read the console error properly. It was a UnityEditor clash. Thank you for your help.

avatar image
2

Answer by KeithK · Apr 12, 2011 at 07:05 AM

Hey Matty

A way around this in JavaScript is to assign your component to a variable of the Type you want. So for your case above, try this:

var menuItem : MenuItem;
menuItem = MenuItems[MenuIndex].GetComponent(MenuItem);

Then you can just use that variable in your function call:

menuItem.MenuItemName;

The generic type call you attempted to change it to's syntax is actually:

MenuItems[MenuIndex].GetComponent<MenuItem>().MenuItemName // No . before the <>

but this is C# only.

I hope this helps you out. I'm a C# programmer myself, so my JS isn't all that confident =P

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 Matthew 5 · Apr 12, 2011 at 11:35 PM 0
Share

Thank you for your post. I attempted assigning the component to a variable earlier, didnt work :( it turned out to be the namespace '$$anonymous$$enuItem'. I didn't read the console error properly. Again thank you for your post, and i like the difference in S and C# syntax in generic casting... a fullstop haha lol.

avatar image
1

Answer by sai59 · Apr 12, 2011 at 07:47 AM

If you are referring to your own component, first possibility I see is that the namespace is clashing. MenuItem is already used by Unity3D engine, so perhaps you want to use another name for your component.

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 Matthew 5 · Apr 12, 2011 at 11:36 PM 0
Share

Thanks for posting man, you are bang on the money. But i had to give the medal to Extrakun, he came first lol! thats what she said. Thanks dude.

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

No one has followed this question yet.

Related Questions

Accessing script variables in objects in an array. 1 Answer

Looping through array to find a specific class? 0 Answers

Array of Transform[] not correctly typed in Javascript 1 Answer

Cannot cast from source type to destination type 1 Answer

Cannot cast from source type to destination type 2 Answers


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