- Home /
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
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.
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?
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 ?
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.
You're welcome, mark my answer as correct (the tickmark below the thumbs) to give me a few karma points for my efforts =)
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
Follow this Question
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