Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question What will be on transform = null?

Discussion in 'Scripting' started by DZZc0rd, Jun 12, 2023.

  1. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    58
    I once made a game on Unity and thought about what would happen if we set the transform variable to null or gameObject to null. Probably the object will be destroyed (only by view)

    Unity version: 2021.3.14f1
     
  2. Yuchen_Chang

    Yuchen_Chang

    Joined:
    Apr 24, 2020
    Posts:
    104
    If you're talking about
    gameobject
    or
    transform
    in your MonoBehaviour, than you can't assign them, because they are getter-only property. If you want to destroy them, use
    Destroy()
    .
    If you're talking about any SerializeField you made, then you can assign null to them, and the script would just simply lose access to the gameobject/transform; no object will be destroyed.
     
    Bunny83 likes this.
  3. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    58
    Oh, thank you for answering on this question