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;
}
}
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With