• 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
0
Question by Kalu · Aug 30, 2011 at 09:53 AM · gameobjectruntime

I can't see runtime GameObject

Well, never mind if I pass for an idiot but this is a moment I'm looking for a reply -__-.

I try to create an object on the fly and display it in the viewer. I have this piece of code, and when I run the apply, I have my Maison1 in addition to the camera and the light already present in the scene (in Hierarchy) ....don't understand what is wrong :(

 void createHouse () 
 {
     GameObject Maison1 = new GameObject ("Maison1");
     if(Maison1)
     {    
         Debug.Log("set position");
         //Maison1.transform.position = new Vector3(0,0,0);
     }
 }

Whatever I do I can not see my house in the viewer

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Waz · Aug 30, 2011 at 10:39 AM

That just creates an empty object that has the name Maison1. Are you meaning to Instantiate a prefab? That would be something like this:

 public GameObject dragHouseHere;
 
 void createHouse () 
 {
     GameObject Maison1 = Instantiate(dragHouseHere);
     if(Maison1)
     {   
         Debug.Log("set position");
         //Maison1.transform.position = new Vector3(0,0,0);
     }
 }

Then drag your house to the variable in the Inspector window.

If you really want to load it by name, it has to be a Resource, then you can:

 GameObject Maison1 = Instantiate(Resources.Load("Maison1"));

Note that resources are loaded for every level in a WebPlayer, so I wouldn't advise using them in that case.

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
avatar image
0

Answer by Kalu · Aug 30, 2011 at 12:28 PM

Ok, I understand that the object that I created is an empty object nome "House" but I get to create an object by getting it directly from my library without having to put it in a public variable or having to make a prefab. I drag & drop a file in my library max and I use it as is, is this possible?

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 Waz · Aug 31, 2011 at 06:51 PM 0
Share

What "library"? If you mean your project, you can only reference things there by name if they are a Resource (I.e. n a folder named Resources. I'll update my answer for that case.

avatar image
0

Answer by Kalu · Aug 31, 2011 at 03:44 PM

No idea, without using public var?

Comment
Add comment · Show 2 · 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 Waz · Aug 31, 2011 at 06:49 PM 0
Share

Sorry, you're using new answers to comment on my answer, so I've not seen them. The "add new comment" button tends to be more effective.

avatar image Kalu · Sep 01, 2011 at 07:52 AM 0
Share

Ok sorry. :)

avatar image
0

Answer by Borgo · Sep 01, 2011 at 01:01 PM

At least in C#, if you do like this:

 GameObject go = new GameObject();
 go.name = "myNewGameObject";

It will create a new empty gameObject named "myNewGameObject" in the scene.
But this way, you will create a gameObject only with a Transform component.

If you want to Instantiate (place an existent object) use the code that Warwick Allison writed.

Comment
Add comment · Show 6 · 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 Kalu · Sep 01, 2011 at 01:59 PM 0
Share

So there is no way to create a gameobject directly get the object from the library project? without public var or other step..?

avatar image Borgo · Sep 01, 2011 at 02:05 PM 0
Share

Yes, you can. See the Warwick answer.
GameObject $$anonymous$$aison1 = Instantiate(Resources.Load("$$anonymous$$aison1"));

avatar image Waz · Sep 01, 2011 at 03:26 PM 0
Share

Note that this name-based asset look-up is really a poor approach, I merely gave the literal answer regardless of the sense of doing such look-ups. Unless you have an actual good reason to use that Resource approach, I would strongly recommend the more direct dependency declarative approach of using an explicit variable rather than some untraceable string and Resources.

avatar image Kalu · Sep 01, 2011 at 03:33 PM 0
Share

I am sorry, i'm not sure to understood, in fact it's safer to make a public var and implement gameobject in this variable by hand rather than go for the model in the resources with paths of access? (sorry, i'm french ..) :)

avatar image Waz · Sep 01, 2011 at 03:56 PM 0
Share

It's safer from a code management standpoint, better in performance, better for WebPlayer scene size $$anonymous$$imisation, and allows tracing the dependency with a single click.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GameObject references runtime script in scene file. Unsure whats wrong 2 Answers

using WWW to upload prefabs/game objects in webplayer... help 1 Answer

PrefabUtility.ReplacePrefab trouble 0 Answers

Assigning sharedMaterial to object built at runtime 2 Answers

Connect two rigidbody gameobjects together at runtime? 0 Answers


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