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 Doughnuts · Sep 06, 2013 at 04:54 PM · c#gameobjecteditorinspector

Custom Inspector: Targets & GameObjects

Hello, I'm having trouble figuring out how to get and edit gameObject data from the selected targets. I basically want to enable and disable certain children of the selected gameobjects depending on an int value on a script inside them;

I serialized the targets which I used to get the int value - that works

  m_Object = new SerializedObject(serializedObject.targetObjects);
  m_TunnelState = m_Object.FindProperty("Slider");
 (Then use "m_TunnelState.intValue" to get int)

But I can't seem to get the gameobjects in a similar way, to do the normal stuff like "gameObject.active = false"

I already tried the following, but clearly it is wrong:

 FloorTile[] Current = (FloorTile[])m_Object.targetObjects;
         foreach(FloorTile Tile in Current)
         {
             Debug.Log(Tile.name);//test
         }
   

('FloorTile' is the script which this editor is being used on)

I get 'InvalidCastException: Cannot cast from source type to destination type.' when selecting and deselecting multiple gameobjects

thanks, sorry for the long post

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
2
Best Answer

Answer by Jamora · Sep 06, 2013 at 07:35 PM

targetObject returns an Object. You'll need to cast that into a GameObject (if it indeed is one) before you can do anything with it. Now, I don't know what FloorTiles is, I assume it's a class that's attached to a GameObject...

 Object[] currentSelection = m_Object.targetObjects;
     foreach(Object obj in currentSelection)
         if(obj is GameObject){
            Debug.Log( ((GameObject)obj).GetComponent<FloorTile>().name );
         }
 

I wrote this straight here, so it's untested. Let me know if it works or if I'm way off.

Comment
Add comment · Show 5 · 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 Doughnuts · Sep 06, 2013 at 07:52 PM 0
Share

This makes sense, Thankyou. However, nothing is traced. It would appear there are no Game objects in the 'Object' to be cast - removing the if statement gives the error Cannot cast from source type to destination type.

So.. Yeah, it doesn't work unfortunately xD Any other ideas?

avatar image Jamora · Sep 06, 2013 at 07:56 PM 0
Share

Did you try to print out only the name of the GameObject? In case there wasn't a FloorTile component... Debug.Log( ((GameObject)obj).name );

Are you selecting GameObjects? put Debug.Log( obj.GetType().ToString() ); after the foreach, before the if, and see if they are GameObjects.

Do you even get anything in the currentSelection? Is its length > 0 ?

avatar image Doughnuts · Sep 06, 2013 at 08:08 PM 0
Share

OOH tracing the type really helped, it turns out that the targetObjects are actually of type FloorTile, so I can just cast the targetobjects as FloorTile and get the game object like that, THAN$$anonymous$$YOU =)

I didn't know you could find the type, that's going to come in useful.

avatar image Jamora · Sep 06, 2013 at 08:12 PM 0
Share

You're welcome, mark my answer as correct (the tickmark below the thumbs) to give me a few karma points for my efforts =)

avatar image Doughnuts · Sep 07, 2013 at 06:20 AM 1
Share

Working code (slight change from original answer)

 private SerializedObject m_Object;
 
 m_Object = new SerializedObject(serializedObject.targetObjects);
 
 Object[] currentSelection  = (Object[])m_Object.targetObjects;
 
 foreach(Object obj in currentSelection)
 {            
 Debug.Log(((ScriptName)obj).gameObject.tag);
 }

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

16 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 avatar image avatar image avatar image avatar image

Related Questions

Edit child in parent list [Custom Inspector] 1 Answer

Best practice for copying Components from one GameObject to another? 3 Answers

Can a GameObject inside of an array be made "accessible" from the editor? 2 Answers

Using script's method from all of the gameobjects that has that script 2 Answers

C# Check If Scripted Gameobject goes Past Variable Gameobject 0 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