Confused about GameObject and Transform with Prefab

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.

2条回复

 ·  添加您的回复
avatar image
6

个解答,截止Bunny83 

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.

评论 ·   隐藏 1 ·  分享

Thank you for the helpful answers. Most 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!

avatar image
1

个解答,截止Wolin 

Because every GameObject has a Transform component but a Transform is not a GameObject. If you want to destroy your GameObject do like this:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值