- Home /
Confused about GameObject and Transform
I have createt a simple Cube und made it a Prefab. noww I want to instantiate that prefab with a javascript. I added a Transform variable to the script and dropped the prefab into that variable, saved the instances in a build-in array. Now I wanted to delete some of these copies in the array. But somehow it is not possible to destroy a Transform. So I made it a GameObject, reassigned the prefab (shows up in the inspector) But if I start the game, the console says, that the prefab is not assigned? What is the Problem? why seems Transform to be the same as GameObject? Why does it work so far with Transform but not with GameObject? How can I delete instantiated Objects from an array (as Transform or GameObject). It makes no sense to me.
Answer by Bunny83 · Jul 04, 2012 at 10:08 AM
Wolin is right, but your problem is that you changed the Transform to GameObject. That's not really a problem, but the prefab you have assigned is no longer assigned. While the variable was a Transform, Unity assigned a reference to the Transform component. When you change the variable type (so it's basically a new variable) you have to assign a gameobject to it. So just drag & drop your prefab again onto the variable.
Btw. some code examples would help.
ps. You should understand the basic concept of gameobjects and components. A GameObject is just a container. The GameObject itself can do almost nothing. Components can be attached to it and give the gameobject some "features". The Transform specifies where it is in the 3D world, a renderer will make it visible, a collider makes it solid (in the sense of physics) and script-components you've written specify arbitrary behaviour.
A GameObject coud be compared with a car-chassis. It's just the basic framework. All other components are attached to the car. Doors, tires, driver's seat, engine, ... all are just components. None of this component is a car, but they belong to it. The same the other way round: a car isn't light, but the car might have one attached.
In Unity every Component has to be attached to a GameObject, they can't live on their own. So if you have a reference to a light component you can "ask" the light: "To which gameobject are you attached to?" by using `light.gameObject`.
The other way is also possible. If you have a reference to a gameobject, you can use either GetComponent() to get a reference to a certain component on this gameobject, or one of the shortcut properties like (transform, light, camera, collider, audio, renderer, rigidbody, ...). Keep in mind that in some situations a certain component could be attached several times (like a tire on a car... you usually have 4). GetComponent always returns the first component or null if there is none at all. GetComponent*s* will return an array of all components on the gameobject.
Thank you for the helpful answers. $$anonymous$$ost of this things where clear to me, but anyway it didn't work. But I found out, that I can assign the GameObject to the script variable by dropping it into the slot of the script or into the slot of the object the script is attached to. so it showed up in the script slot correctly but the object slot still said "None". So I dropped it there to and it worked out fine.
Anyway - thanks for the informatio again!
Answer by Wolin · Jul 04, 2012 at 09:00 AM
Because every GameObject has a Transform component but a Transform is not a GameObject. If you want to destroy your GameObject do like this:
public Transform yourPrefab;
public void destroyIt() { Destroy(yourPrefab.gameObject); }