x


How to create a game object through a function

I'm trying to figure out how I can get this function to work as I often need to use it and using copy/paste all the time is getting messy.

var infoSymbol : GameObject ;
var infoSymbolButtonComponent : UIButton;

function DoesPageNeedInfoSymbol()
{
    // First page
    if (currentPageNumber == 0)
    {
       MakeInfoSymbol(infoSymbol,Vector3(196.3,-49.8,202));


       // Get the reference to ezgui component of the button
       infoSymbolButtonComponent = infoSymbol.GetComponent(UIButton);

       //Set the method to invoke up
       infoSymbolButtonComponent.scriptWithMethodToInvoke = this;
       infoSymbolButtonComponent.methodToInvoke = "extra_1"; 


}

function MakeInfoSymbol(symbolGameObjectName : GameObject, Pos : Vector3)
{
    symbolGameObjectName= Instantiate(Resources.Load("infoSymbol", GameObject));
    symbolGameObjectName.renderer.sharedMaterial.mainTexture = Resources.Load("loaded_book_1", Texture2D);
    symbolGameObjectName.transform.position =Pos;
    infoPageNumber = currentPageNumber;    

}

But I keep getting an error saying The variable infoSymbol of 'bookManager' has not been assigned. From this line:

    infoSymbolButtonComponent = infoSymbol.GetComponent(UIButton);
more ▼

asked Sep 14 '11 at 09:35 AM

apple741 1 gravatar image

apple741 1
3 5 5 10

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Unless you assign a GameObject to infoSymbol at any point in your script (as I see you do in the commented out line just above) you will always get a null reference exception here. Try adding

infoSymbol = symbolGameObjectName;

at the end of your MakeInfoSymbol() function. Better yet, have MakeInfoSymbol() return a GameObject, and then assign infoSymbol to it in your DoesPageNeedInfoSymbol function, like so:

function MakeInfoSymbol(symbolGameObjectName : GameObject, Pos : Vector3)  :  GameObject
{
    symbolGameObjectName= Instantiate(Resources.Load("infoSymbol", GameObject));
    symbolGameObjectName.renderer.sharedMaterial.mainTexture = Resources.Load("loaded_book_1", Texture2D);
    symbolGameObjectName.transform.position =Pos;
    infoPageNumber = currentPageNumber;    
    return symbolGameObjectName;
}

you also may want to make the symbol prefab be a public GameObject var at the top of your script so that it can be assigned in the editor, and then instantiated without using awkward string-literal resource loading.

more ▼

answered Sep 14 '11 at 09:49 AM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

Thanks syclamoth, I dont want to add infoSymbol = symbolGameObjectName; directly so I can use other names like infoSymbolTwo etc.. I'm interested in your last approach with returning a game object. What would I do at that point in DoesPageNeedInfoSymbol() ?

Sep 14 '11 at 10:13 AM apple741 1

In DoesPageNeedInfoSymbol(), you would have a line which goes something like

infoSymbol = MakeInfoSymbol(infoSymbol, position);

Instead of using Resources.Load(whatever), you should consider putting a prefab at the top of your script, and instantiating that.

Sep 14 '11 at 10:21 AM syclamoth

I seem to still get the infoSymbol no unassigned error even with this method? I'm was hoping to stick with resources.load so I can control the amount of memory used as this if for ios. Thanks again :-)

Sep 14 '11 at 10:36 AM apple741 1

I can't say this often enough- but to quote Donald Knuth, "premature optimisation is the root of all evil". There's no need to make things too complicated for yourself before you even get it working, and in the end it probably won't make that huge of a difference unless you are dealing with a seriously huge amount of data. By the sounds of things, the Resources.Load bit isn't returning an object properly, and as such the object never gets instantiated- Look at all your error messages, and try doing it in a different way to see if it works better.

Sep 14 '11 at 10:41 AM syclamoth

As far as I can tell, there is no real reason to use resources.Load for iOS devices, especially if the resource in question is a prefab, as opposed to say a texture or 3d model. When the project gets built, it only includes things which are being used in the project, and assigning objects in the inspector is one of the ways in which it does this.

Sep 14 '11 at 10:44 AM syclamoth
(comments are locked)
10|3000 characters needed characters left

Select the object you have attached this script onto, then in the inspector you can select the info-object needed for the script.

You can also find gameobjects by code, with GameObject.Find("{name of it}");

If you need to spawn it, you can just make a variable like: var go:GameObject = new GameObject();

more ▼

answered Sep 14 '11 at 09:49 AM

BerggreenDK gravatar image

BerggreenDK
2.4k 54 62 75

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5069
x3454
x1956
x840

asked: Sep 14 '11 at 09:35 AM

Seen: 1183 times

Last Updated: Sep 14 '11 at 10:55 AM