您的位置:首页 > 移动开发 > Objective-C

Confused about GameObject and Transform with Prefab

2017-01-09 16:28 661 查看


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条回复

 · 添加您的回复

排序: 







6

个解答,截止Bunny83 · 2012年07月04日 10:08

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 · 分享



fschaar · 2012年07月04日
11:23 0

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!




1

个解答,截止Wolin · 2012年07月04日 09:00

Because every GameObject has a Transform component but a Transform is not a GameObject. If you want to destroy your GameObject do like this:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐