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 :frowning:

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

4 Answers

4

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.

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?

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.

–

No idea, without using public var?

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.

–

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.

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

–

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.

–

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

–