Ignoring a GameObject causes the specified object to destroy Transform?

New for Unity and C #

Actually this is just a small problem that I'm interested in ... I ran into it, trying to configure this code in an attempt (failed) to get it to work. I tried to get this code to work for several hours.

In any case, when this code is executed, there is only one error, but it appears 3 times. It says: “It is not possible to destroy the Transform component“ Pillar1. ”If you want to destroy the game object, please call“ Destroy ”on the game object. Destruction of the transformation component is not allowed.”

The first time I got THIS.

using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlatformGenerator : MonoBehaviour { public GameObject Single; private GameObject NewPillar; private int PillarCount; private bool run; private int px; private int py; private int pz; void Start () { px = 0; py = 0; pz = 0; bool run = true; PlatformCreate (); } void Update () { if (run) { PlatformCreate (); if (PillarCount == 3) { run = false; } } } void PlatformCreate () { PillarCount +=1; Single.transform.position = new Vector3 (px, py, pz); NewPillar = Instantiate (Single, Single.transform); NewPillar.name = "Pillar" +PillarCount; px += 2; } } 
+5
source share
3 answers

1) Using the following operators causes an unwanted result or throw error -

 NewPillar = Instantiate (Single, Single.transform); 

OR

 NewPillar = Instantiate (Single); NewPillar.transform.parent = Single.transform; 

2) You can get around this using the following code -

 NewPillar = Instantiate (Single, Single.transform.position, Single.transform.rotation); 

Explanation: Single is a precast, not an actual object in the scene. A transform is also a component that determines the position, rotation, and scale of an object in a scene. According to Unity3D, since Single is a prefab, its conversion component is directly disabled to prevent data corruption. That is why we get an error when using the operators in paragraph 1 above.

But we can use the location, rotation and scale data stored in the prefab transform component. This allows us to use the statement in paragraph 2 above.

+2
source

You cannot invoke Destroy on Transform type links. What kind of error says that you need to pass GameObject to the Destroy method, not Transform.

I assume that the part of the code that "kills" the transformation is missing, but in any case, my hunch is this:

  Destroy(transform); // -1 

or

  Destroy(pillar1.transform); //Where pillar1 is a Gameobject -2 

or

  Destroy(pillar1); // Where pillar1 is a Transform -3 

Replace

-1 s

  Destroy(gameObject); 

-2 s

  Destroy(pillar1); //Where pillar1 is a Gameobject -2 

-3 C

  Destroy(pillar1.gameObject); 
+1
source

The error indicates that the Pillar1 object already exists, and therefore we assume that this is one of the subsequent PlatformCreate calls.

Call Instantiate includes all children. This way you clone Single into Pillar1 and again add a new game object to Single . In the next call, both of them are cloned. So Pillar2 will be Single and Pillar1

Although I do not understand why this should not work, I suspect an internal error. Are you sure that this is what you want (maybe you just wanted to indicate the position and forgot the rotation (s. Instantiate )? This is what you wanted, then try to break the Instantiate process into 2 stages:

  • Instantiate call without parent specification
  • pillar2.SetParent(Single)
0
source

All Articles