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 instancesin 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:
相关文章推荐
- About SWFObject generated by Flash Professional, and the HTML tag with clsid
- Succeeding with Object databases: a practical look at today's implementations with Java and XML
- Error:Execution failed for task ':app:transformClassesAndResourcesWithProguardForDebug'. > Job faile
- studio混淆apk打包错误:app:transformClassesAndResourcesWithProguardForRelease'. > java.ioe异常
- 论文笔记:Mastering the game of Go with deep neural networks and tree search
- 【Codeforces Round #429 (Div. 1) B】Leha and another game about graph
- Error:Execution failed for task ':app:transformClassesAndResourcesWithProguardForOfficialRelease'
- [转]Working with user roles and permissions in SharePoint Object Model
- 实例化Transform对象与GameObject对象时调用方法时的区别
- GameObject.Find()、Transform.Find查找游戏对象
- Fast and Accurate CNN Object Detector with Scale Dependent Pooling and Cascad Rejection Classifiers
- Object-Oriented Analysis and Design : Understanding System Development with UML 2.0
- Mastering the game of Go with deep neural networks and tree search
- Android Error:Execution failed for task ':app:transformClassesAndResourcesWithProguardForRelease'.
- Concise steps to Building and Installing the FIPS Object Module with OpenSSL (Unix/Linux)
- Android 混淆报错app:transformClassesAndResourcesWithProguardForRelease
- Data Structures and Algorithms with Object-Oriented Design Patterns (1)
- Data Structures and Algorithms with Object-Oriented Design Patterns (4)
- Debugging with GDB: Introduction to Commands, Print and Print-Object
- GameObject.FindGameObjectsWithTag 通过标签查找游戏对象列表