TransWikia.com

I'm studying the C# constructor and destructor with Unity, but I don't know why it works like this

Stack Overflow Asked by JunYoung Yoon on November 24, 2021

I’m studying C#’s Constructer and destructor with unity component system.
I’m sorry if the English of this question is weird. I used a translator cause I am not good at English.
The log output came out like this.

  1. The Constructor log was displayed without pressing the play button. Why?
    Constructor Log(Did not clicked play button)
  2. When I pressed the play button, I saw a log of something being created and immediately disappearing. I didn’t write a code to create an object after the game started, where does this phrase run?Game play first log

This is my code.

project working structure

Pressing the space bar brings the pre-made pre-fab to the game world,
and Prefab has a component that attached to test the constructor and
destructor.

CubeFactory.cs / when Press the space bar, It creates Prefab.
this component was attached to "GameObject" Gameobject

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubeFactory : MonoBehaviour
{
    public GameObject obj;
    private int pos = 1;
    void Start()
    {   
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Instantiate(obj,new Vector3(pos,0,0),Quaternion.identity);
            pos++;
        }
    }
}

ClassTest.cs / Component for Testing Constructor and Destructor. It attached Cube prefab.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ClassTest : MonoBehaviour
{
    public ClassTest(){
        Debug.Log("I was born!");
    }
    ~ClassTest(){
        Debug.Log("I Died x0x");
    }
}

This is my project file. (I’m sorry to compress it up. I haven’t learned how to use Git yet.)<
https://github.com/Scincy/UnityStudy

One Answer

Please change your ClassTest.cs script to use simpler functions. Never use constructors and finalizers in Unity. What I understood from your logic is that when you press Spacebar, an object is instantiated. The ClassTest.cs is attached to this object. Better use:

  void Start()
    {
      Debug.Log("I was born!"); //Will be called when the gameObject is active for the first time
    }

Now you also want to know if the gameObject is destroyed, you can do this with another script attached to an always-active gameObject such as:

public GameObject cube;

    void Update()
    {
        cube= GameObject.Find("Cube(Clone)"); //Depending on how your object is called
        if(cube == null)
        {
         Debug.Log("Either not created or is destroyed");
        }
     }

I have used Update() which is called every frame. You could use a different function that you would like to call from another script only once.

Answered by user11458208 on November 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP