Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Discussion Will the Transform Component be removed if we set a transform to null? transform=null

Discussion in 'Scripting' started by pvrapptakkarjr, Nov 2, 2022.

  1. pvrapptakkarjr

    pvrapptakkarjr

    Joined:
    Nov 19, 2021
    Posts:
    1
    Code (CSharp):
    1. void updateGrid() {
    2.     // Remove old children from grid
    3.     for (int y = 0; y < Playfield.h; ++y)
    4.         for (int x = 0; x < Playfield.w; ++x)
    5.             if (Playfield.grid[x, y] != null)
    6.                 if (Playfield.grid[x, y].parent == transform)
    7.                     Playfield.grid[x, y] = null;
    8.  
    9.     // Add new children to grid
    10.     foreach (Transform child in transform) {
    11.         Vector2 v = Playfield.roundVec2(child.position);
    12.         Playfield.grid[(int)v.x, (int)v.y] = child;
    13.     }      
    14. }
    1. Firstly, what exactly happens when we set a transform to null?
    2. Then, will all the properties of the game object such as transform and other attached objects be reset or removed if we set it to null?
    3. At last, pls explain the above method clearly, I don't have a clear understanding abt it.
    (Full game and code: Tetris Tutorial)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    It's just a variable, you can set it to null.

    If you're talking about the
    Component.transform
    shortcut, that is NOT what you are setting above, and you cannot set that. It is read only:
    Screen Shot 2022-11-02 at 12.29.51 PM.png

    It appears to loop over a 2D array, perhaps of Transforms (I infer this from the .parent property), and checks to see if each one is parented to this script's GameObject's Transform, and if so, sets that grid cell to null.

    It sounds like perhaps you failed to do Step #2 in following tutorials?

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.


    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
    Bunny83 likes this.
  3. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,393
    grid
    is a variable. It can hold references to Transform components.

    You can assign a new reference into a variable using the
    =
    operator.
    Code (CSharp):
    1. GameObject player = GameObject.Find("Player");
    Since variables just hold references to components, you can have multiple variables reference the same component instance.
    Code (CSharp):
    1. GameObject player = GameObject.Find("Player");
    2. GameObject alsoThePlayer = player;
    When you assign a
    null
    value into a variable, it does not destroy (or in any other way affect) the object whose reference the variable held previously. It just clears the variables, so it will hold no reference to any component.

    If two variables hold a reference to the same component, and you assign a null value to one of the variables, it also does not affect the reference held by the other variable in any way.
    Code (CSharp):
    1. GameObject player = GameObject.Find("Player");
    2. GameObject alsoThePlayer = player;
    3. player = null;
    4. Debug.Log(alsoThePlayer.name); // Prints "Player"
    To actually remove a component from a GameObject, you would need to pass a reference to the component to the Destroy method.

    After this all variables that held a reference to the component that was destroyed will be pointing at nothing (
    null
    ).
    Code (CSharp):
    1. Collider collider = gameObject.AddComponent<Collider>();
    2. Collider alsoTheCollider = collider;
    3. Destroy(collider);
    4. Debug.Log(alsoTheCollider == null); // Prints "True"
    Being a 2D array, the
    grid
    variable can hold multiple references to Transform components instead of just one, but other than that the logic still works the same.
    Code (CSharp):
    1. void TicTacToe()
    2. {
    3.     var x = new GameObject("x").transform;
    4.     var o = new GameObject("o").transform;
    5.  
    6.     // [ ][ ][ ]
    7.     // [ ][ ][ ]
    8.     // [ ][ ][ ]
    9.     var grid = new Transform[3,3];
    10.  
    11.     // [ ][ ][ ]
    12.     // [ ][ ][ ]
    13.     // [x][ ][ ] <-
    14.     grid[0, 0] = x;
    15.  
    16.     // [ ][ ][ ]
    17.     // [ ][o][ ] <-
    18.     // [x][ ][ ]
    19.     grid[1, 1] = o;
    20.  
    21.     // [ ][ ][ ]
    22.     // [ ][o][ ]
    23.     // [x][x][ ] <-
    24.     grid[1, 0] = x;
    25.  
    26.     // [ ][ ][ ]
    27.     // [ ][o][ ]
    28.     // [x][x][o] <-
    29.     grid[2, 0] = o;
    30.  
    31.     // [ ][ ][ ]
    32.     // [ ][o][ ]
    33.     // [x][x][ ] <-
    34.     grid[2, 0] = null;
    35.    
    36.     // [ ][ ][ ]
    37.     // [ ][o][ ]
    38.     // [x][x][ ]
    39.     x = null;
    40.     o = null;
    41.  
    42.     // [ ][ ][ ]
    43.     // [ ][ ][ ]
    44.     // [ ][ ][ ]
    45.     x = GameObject.Find("x").transform;
    46.     o = GameObject.Find("o").transform;
    47.     Destroy(x);
    48.     Destroy(o);
    49. }
     
    Bunny83 likes this.