Creating a GameObject and assign a geometry

Hello Guys’

I have a geometry uploaded in the project but not yet visible because it has not a GameObject.

I’m writing a simple script that as soon as a button is clicked, creates a GameObject, assigns the pre-loaded geometry and that’s it. The geometry appears/disappears…

But I don’t get,… to add a geometry which is in the Project but not in the Hierarchy can I use AddComponent?!

Many thanks.
GC.

The geometry appears then on the next frame disappears? If so, it sounds like you have some other script somewhere that’s causing it to delete itself.

Usually, if you want to add geometry into your scene, you have your GameObject already created (“prefab”), then use the Instantiate call to create a copy of it. You might also use Resources.Load to read in that asset:
var instance : GameObject = Instantiate(Resources.Load(“enemy”));

Well no my geometry doesn’t appear and disappear as I’ve imported it only in the Project - Window… and not in the Hierarchy - window.

Uhm… I guess in the ‘free’ the prefabs don’t work :frowning:

GC>

They certainly do work in the free version, Click the ‘Create’ button on the project window, then Prefab. Drag your object onto that new prefab and the icon should turn blue. Now you can use the instantiate command to call it into the game.

Hi Jdeuel, thank you, what I want to do is using the renderer.enabled flat to enable/disable the rendering of the geometry. Now, this is my code where ‘render’ is simply a boolean and I made appear the geometry but when I click a button it doesn’t disappear. Can you help me in finding the mistake? :)) Thanks!

private var render: boolean = true;
private var mainObject: GameObject;
var skin: GUISkin;

function Awake() {
Debug.Log(“Awake”);

mainObject = Instantiate(Resources.Load(“Petrie”, GameObject));
mainObject.name = “Room”;
mainObject.AddComponent(MeshRenderer);

}

function Update () {

}

function OnGUI() {

if (GUI.Button (Rect (10,10,150,100), “Add Geometry”,“B_HOME”)) {

var game = GameObject.Find(“Room”);
Debug.Log(“GAME?” + game);

if(game != null) {
game.GetComponent(MeshRenderer).enabled = render;
render = !render;
Debug.Log("Render: " + render);
}
}

GUI.skin = skin;
}

You need to assign the true/false back to the renderer; your lines are just backwards :stuck_out_tongue:

render = !render;
game.GetComponent(MeshRenderer).enabled = render;

That’s weird… as it doesn’t work :frowning: My object is still not rendered… I wonder is the “if (GUI.Button (Rect (10,10,150,100), “Add Geometry”,“B_HOME”)) {” called both when I press the button and the I release it?!

GC.

Trace your Debug.Logs, do you see it being changed multiple times? Or do you ever just see it being set to true and never false?