Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by fschaar · Jul 04, 2012 at 08:53 AM · javascriptgameobjecttransformdestroy

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
7

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fschaar · Jul 04, 2012 at 11:23 AM 0
Share

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!

avatar image
2

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); }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Trying to make a simple inventory: 0 Answers

Co Routine 2 Answers

Add a Transform/GameObject to the selection from an editor script 2 Answers

How to destroy a transform's parent object. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges