Search Unity

How to deParent a GameObject in the Hierarchy using Visual Scripting?

Discussion in 'Visual Scripting' started by literacy, Jul 4, 2022.

  1. literacy

    literacy

    Joined:
    Nov 14, 2021
    Posts:
    56
    I am trying to unParent a GameObject, and move it into the root of my Hierarchy. I've seen various mentions that this C# snippet should do the trick:

    GameObject.transform.parent = null;

    I tried to create a Visual Scripting version of this C# code here

    unknown (2).png

    But I keep getting the error that "P cannot be Null".

    unknown (3).png

    Any suggestions how to set the parent of a node to Null using Visual Scripting? Thanks!
     
  2. munchmo

    munchmo

    Joined:
    May 20, 2019
    Posts:
    86
    I think you need to just leave the node unconnected so it says "None"
     
  3. literacy

    literacy

    Joined:
    Nov 14, 2021
    Posts:
    56
    When I try that, it gives the same error message?
    None.png
     
  4. literacy

    literacy

    Joined:
    Nov 14, 2021
    Posts:
    56
    I must be missing something... just feels like there must be a way to move a Child GameObject into the root of the Unity Hierarchy!
     
  5. ericb_unity

    ericb_unity

    Unity Technologies

    Joined:
    Nov 24, 2017
    Posts:
    167
    if the script with the unparent action is on the same object you want to unparent, doing this will do the job.
    upload_2022-7-6_9-48-5.png

    This is the one I'm using.
    upload_2022-7-6_9-49-18.png

    If you want to pass an object from a variable, the setup, will look like this.
    upload_2022-7-6_9-51-13.png
     
  6. munchmo

    munchmo

    Joined:
    May 20, 2019
    Posts:
    86
    Yep, that's exactly how I set it up when I tested it and it worked fine but according to OP it didn't work for them.
     
  7. ericb_unity

    ericb_unity

    Unity Technologies

    Joined:
    Nov 24, 2017
    Posts:
    167
    What is OP? Object parent?
     
  8. literacy

    literacy

    Joined:
    Nov 14, 2021
    Posts:
    56
    @ericb_unity - Thank you for the response! I will try the nodes you sent. My nodes were orange just like yours, and I couldn't get them to change color.

    After losing a few days to this, I decided to just destroy the GameObjects and Instantiate new ones. Glad to hear that you can deparent a Child Node using Visual Scripting!
     
  9. ericb_unity

    ericb_unity

    Unity Technologies

    Joined:
    Nov 24, 2017
    Posts:
    167
    The issue with your solution if you decide to continue to destroy and instantiate, is that you will generate garbage collection and start to get frames taking lot of time to process.
     
  10. munchmo

    munchmo

    Joined:
    May 20, 2019
    Posts:
    86
    OP is shorthand for Original Poster, meaning the person who asked the original question.
     
    ericb_unity likes this.
  11. GreyEyedBeast

    GreyEyedBeast

    Joined:
    Jan 6, 2013
    Posts:
    7
    This method worked for me. not sure if this is the same reason his was orange but for me mine was orange when the game wasn't active because the variable was set somewhere else in my node graph while the game is running.