Ashton Sipes
Ashton Sipes

Reputation: 67

Instantiating a GameObject causes said object to have its Transform destroyed?

New to Unity and C#

This is actually just a small issue that I'm curious about... I ran into it while tweaking this code in a (failed) attempt to make it work. I have been trying to get this code to work for a few hours now.

Anyway, when this code is executed, there is only one error, but it appears 3 times. It says "Can't destroy Transform component of 'Pillar1'. If you want to destroy the game object, please call 'Destroy' on the game object instead. Destroying the transform component is not allowed."

First time I've gotten THAT.

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;
}
}

Upvotes: 5

Views: 827

Answers (3)

Mukesh Saini
Mukesh Saini

Reputation: 1498

1) Using following statements causes undesired result or throw error -

NewPillar = Instantiate (Single, Single.transform);

OR

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

2) You can bypass this by using following code -

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

Explanation : Single is a prefab and not an actual object in the scene. Also transform is a component which determines the Position, Rotation and Scale of object in the scene. As per Unity3D, since Single is a prefab, using it's transform component directly is disabled to prevent data corruption. Which is why we get error while using statements in point 1 above.

But we can use the position, rotation and scale data stored within the prefab's transform component. Which lets us use the statement in point 2 above.

Upvotes: 2

Kay
Kay

Reputation: 13146

The error indicates that there is an object Pillar1 already and so we assume it is one of the subsequent calls to PlatformCreate.

The call to Instantiate includes all children. Thus you clone Single to Pillar1 and reparent the new game object to Single. In the next call both of them are cloned. So Pillar2 will be Single and Pillar1

Although I don't see why this should not work, I suspect an internal error. Are you sure this is what you want (maybe you just wanted to specify the position and forgot the rotation (s. Instantiate)? If it is what you wanted then try to split the Instantiate process into 2 steps:

  1. Call Instantiate without parent specification
  2. pillar2.SetParent(Single)

Upvotes: 0

Farhan
Farhan

Reputation: 1278

You can not call Destroy on references of type 'Transform'. What the error is saying that you need to pass the GameObject to the Destroy method, and not the Transform.

I am guessing that the part of the code 'destroying' a transform is missing, but in any case, my guess is like 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 With

    Destroy(gameObject);

-2 With

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

-3 With

    Destroy(pillar1.gameObject);

Upvotes: 1

Related Questions