Want to Destroy Prefabricated Game Objects

Asked 1 years ago, Updated 1 years ago, 63 views

I have a question at Unity.
Currently, I've tried to create multiple game objects in an instance and try to destroy them, but I'm having a hard time.

In addition, the following error has occurred, so this may be the cause, but I am in a state of confusion.

error message

The referenced script on this Behavior (Game Object 'Sphere') is missing!
UnityEngine.Object: Instantiate (GameObject, Vector3, Quaternion)
clone —Update() (at Assets/clone.cs:45)

By the way, line 45 is as follows.

Instantiate(Sphere, new Vector3(i, 7.6f, -2), Quaternion.identity);

source code

// Use this for initialization
public GameObject Sphere;
public float timeout;
public static Vector 3 mousePosition;

// float var;
// var = Random.Range (0.0f, 9.0f);

void Start() {
}

void DelayMethod()
{
    Debug.Log("Delay call");
}

void Update()
{
    inti = Random.Range(0,10);
    timeout -=Time.deltaTime;

    if(timeleft<=0.0f)
    {
        timeout = 0.5f;
        Instantiate (Sphere, new Vector3(i, 7.6f, -2), Quaternion.identity);
    }

    /* if (Input.GetMouseButtonDown(0))
      {

      RaycastHit 2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mouthPosition), Vector2.zero);
      Debug.Log("Test");
      if(hit.collider!=null)
      {
      Debug.Log("Test");
      if(hit.collider.gameObject==Sphere) Destroy(Sphere);
      }
      }*/
}

unity3d

2022-09-30 18:14

2 Answers

First, let's understand how Unity works.

GameObject is the very basic game object of Unity.

A prefabricated game object is a model.
Programmer Description: Prototype

Instantiate() clones game objects that are models.
Programmer's Description: Cloning with Prototype I clone it by serializing it and deserializing it.UnityEngine.Object Inheritance Class Restores Reference Specialization

MonoBehavior is a script that attaches to a game object.
Programmer's description: Behavior tied to GameObject.
·GameObject itself has only coordinates, orientation, and parent-child relationships between GameObjects
·Start() when the attached GameObject is replicated by instantiating
·Update() every time the game goes one frame further
·GameObject generated by Instantiate is not updated() in the frame except for the first frame at the start of Unity because it is effectively instantiated in the update step.Example: STG generates bullets, for example)

If you attach a feature to GameObject (Prefab) on the editor (Collider, self-made Behaviour, etc.), the public field appears on the editor and you can set the parameters.US>Alias Checker
Programmer Description:
Instantiate() moment, set as follows: ·Constructor
·Reading inspector values
·Call Awake()
·Instantiate() is over and the return value is returned

Let's go back to the question after understanding the above.There are probably two possible causes:

First

publicGameObjectSphere

This is the public field of MonoBehavior, so I think it is displayed on the editor's inspector.
If this field is prefabricated on the Editor or if GameObject is imported from somewhere on the program, this value should be null.
Of course, null (empty) cannot be instantiated (although some versions may have a dummy GameObject configured on their own…)

Second

The referenced script on this Behavior (Game Object 'Sphere') is missing!

This is a common warning, and if you look at the prefabricated version that you specified for Sphere, it will appear when there is an inconsistency in the attachment because the script itself has disappeared or changed its name.

I think one of the reasons is the cause, so please check again.

Unity can make a significant part of the GUI, but I think it is difficult to make C# knowledge at zero, so
Variables, types, constructors, unconfigured variables (null), and variables with new instances should be read diagonally.


2022-09-30 18:14

The Sphere in line 45 is empty.

Do I have to prefabricate the second line of public GameObject Sphere; Sphere?

Drag the prefabricated Sphere into the Sphere column of the inspector that is providing information for this script

I need to connect it.  I don't think this is an attachment.

Look into that area again...


2022-09-30 18:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.