- Home /
Cloning GameObjects with custom inspector script attached
I am running into a little problem on IOS with this particular case.
I have a GameObject which has many children/sub-children, and the root object has a custom inspector script that I wrote attached to it. What the script does is loop through all children/sub-children found on that GameObject and displays them as a list in the inspector, each with a checkbox next to them, which allows me to activate/deactivate each children easily without having to look for them, select them and enable/disable them manually.
On Start(), I run through all my "special" objects and I clone them by the amount of clones needed, at their new position. This works fine when playing in the editor, each child which was deactivated on the original object is also deactivated on the clone. The problem happens when I run the game on IOS, the children on the cloned objects are not deactivated, they're all enabled and the cloned object doesn't match the original object.
So I tried to manually loop through all the children on the original object, get their enabled state, and assign it to the clone's children enabled states. But that didn't work as expected, it seamed that only the first child found was successfully enabled/disabled accordingly, but it stops there and doesn't go through all the other children even though my function to get/set states is recursive.
I know that the problem I'm running into is that since the game is running on IOS, "custom inspector scripts" do not work like they do while testing in the editor, but I'm not sure how to get around that problem. I know I'm doing something wrong, but I can't figure out what exactly!.
Sorry for the lengthy post, but it's kinda hard to explain in a few lines. I can post the code from my custom editor/inspector script and the code which clones all special objects if needed. I am hoping someone will be like "Oh yeah you have to do X for this to work on IOS" hahaha.
Thanks guys! Stephane
Hi Stephane! It seems like the changes that you are making are not being persisted. Are you using SerializedObject
and SerializedProperty
in your custom inspector? If you aren't then you should be using EditorUtility.SetDirty
. I suspect that you would experience the same issue if you were building your game to run on $$anonymous$$ac/Windows/Android, or even if you were to restart Unity and run in the editor.
If you are not using either of those methods for ensuring that data is persisted I would be happy to include an example of this in an answer below. I am not sure if SerializedObject
can be used in conjunction with that particular property, but EditorUtility.SetDirty
will definitely work (because I use it for that)
Sorry for so many comments, please also let me know which version of Unity you are using because this will affect my answer.
Hey man, thanks a lot for your help. After looking into my "recursive" function a little more, I found out that it wasn't working correctly so it wasn't going through all the children and setting them accordingly.
But to answer your question, yes I am using SerializedObject and SerializedProperty in my custom inspector, as well as EditorUtility.SetDirty when I detect a change.
So far so good, I build to PC and ran the game and it works fine, so I'm pretty sure it will work as well on iPhone.
Thanks again!
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
CustomEditor show an instance of a Class inside a Class 0 Answers
Update editorscript if gameobject is manipulated. 1 Answer
Unity (clone) not destroying 2 Answers
[Unity Editor] Emulate Touch Input - Still Asking 12/10 1 Answer