playMaker

Author Topic: Spawn Object then disable its child in runtime.  (Read 1913 times)

Aaddiction

  • Full Member
  • ***
  • Posts: 166
Spawn Object then disable its child in runtime.
« on: May 08, 2015, 04:47:45 PM »
How to spawn an object (prefab from global variable) and disable one of its children?
What it comes to mind to me is set tag to the child object I want to disable and then use find with tag and deactivate this object in the next state? However, it does not seem to work with me.

Do you think there is more elegant way to do this?
« Last Edit: May 08, 2015, 07:19:57 PM by Aaddiction »

wheretheidivides

  • Sr. Member
  • ****
  • Posts: 496
Re: Spawn Object then disable its child in runtime.
« Reply #1 on: May 08, 2015, 04:57:58 PM »
What I do is:

Instantiate
Enable the object recursively.  If the child is off, then you can not turn it on or off.
Find the object by name
Store the object into a variable
Deactivate variable.


Doing it this way you can turn on and off (activate).  if it is deactivated, you can not activate it.  It won't find it.  However, putting into a global variable it will always be available.
« Last Edit: May 08, 2015, 05:00:35 PM by wheretheidivides »

Aaddiction

  • Full Member
  • ***
  • Posts: 166
Re: Spawn Object then disable its child in runtime.
« Reply #2 on: May 08, 2015, 08:12:10 PM »
Many thanks. I'm still little a bit confused, but I'll keep digging. Atm, when I de-activate the object variable it's still stays activated, but when I re-instantiate it, its deactivated. I guess it affects the variable, and the object in the scene is actually not the variable, but a separate object.

wheretheidivides

  • Sr. Member
  • ****
  • Posts: 496
Re: Spawn Object then disable its child in runtime.
« Reply #3 on: May 08, 2015, 10:37:06 PM »
1) Create object (store in GO-P1-Tank)
2) Activate the game object recursivesly (in case something is off).
3) Assign game object names (children) to global variable.  Must know the names from the prefabs.
    P1-Tank-Body (Prefab name of child) ------ GO-P1-Tank-Body (Global variable).
4) Just activate or deactivate game object variable for child (GO-P1-Tank-Body).