5

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;
}
}
  • For some reason it seems to have to do with having Single.transform in the Instantiation... removing and only having Single doesn't bring up the error, but I still would like to know why. – Ashton Sipes May 04 '17 at 10:50
  • @Programmer I voted to reopen this question as there is definitely no call to `Destroy`. See the comment from Ashton and my answer below. I agree that most questions of this type are kind of trivial beginners' mistakes but this one seems to be different and quite interesting of course – Kay May 04 '17 at 11:49
  • @Kay It must be be called somewhere in the **other** scripts. I've seen people post wrong code that has nothing to do with their problems in their questions. The reason for that error is when you try to destroy a `transform`. For example, `Destroy(transform)`I will reopen if OP can prove otherwise. – Programmer May 04 '17 at 11:52
  • @OP I suggest you look everywhere in your code for where you used `Destroy(transform)` or the Destroy function. That's what you should post in your question. It could be inside any script. – Programmer May 04 '17 at 11:54
  • @programmer please reopen this question as this is not duplicate of what you are assuming it to be. I'll add answer for this question. – Mukesh Saini May 04 '17 at 14:06
  • @MukeshSaini I insist that the problem occur when `Destroy` is used on a `transform`. I am waiting to hear from OP on this. If OP says he looked but didn't find the use of Destroy function on a transform then I will re-open it right away. – Programmer May 04 '17 at 14:09
  • @programmer, have you ever used `Instantiate(prefab, prefab.transform)` statement in unity app? And I agree that another problem occur when `Destroy` is used on a `transform`, but that is not the case here. – Mukesh Saini May 04 '17 at 14:11
  • @MukeshSaini Yes and it shouldn't throw the *"Can't destroy Transform component of*" exception. How about you, have you? Did you see such error when you did? – Programmer May 04 '17 at 14:17
  • 1
    @programmer, that doesn't yield the desired result. Also look at @Ashton's comment at top ____ he just wants to know WHY? – Mukesh Saini May 04 '17 at 14:22
  • 1
    @MukeshSaini `Destroy(transform);` is mainly the the cause of that error. I tested `Instantiate (Single, Single.transform);` before closing this and the error did not occur. I just tried again about 15x and the error came up. It is random. It happens sometimes. So yeah, `Instantiate (Single, Single.transform);` causes that too. It looks like the native side does not understand that and is trying to do `Destroy(transform)` under the hood. Re-opened. Please put your answer. – Programmer May 04 '17 at 14:45

3 Answers3

2

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.

Mukesh Saini
  • 1,488
  • 1
  • 9
  • 16
  • The OP did not mentioned that Single is a prefab. But I assume this is the reason as it is the only explanation that makes sense +1 – Kay May 04 '17 at 15:34
1

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);
Farhan
  • 1,000
  • 1
  • 11
  • 22
0

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)
Kay
  • 12,918
  • 4
  • 55
  • 77