- Home /
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);
Answer by syclamoth · Sep 14, 2011 at 09:49 AM
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.
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() ?
In DoesPageNeedInfoSymbol(), you would have a line which goes something like
infoSymbol = $$anonymous$$akeInfoSymbol(infoSymbol, position);
Ins$$anonymous$$d of using Resources.Load(whatever), you should consider putting a prefab at the top of your script, and instantiating that.
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 :-)
I can't say this often enough- but to quote Donald $$anonymous$$nuth, "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.
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.
Answer by BerggreenDK · Sep 14, 2011 at 09:49 AM
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();
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
accessing a variable from one script in another with Unity 1 Answer
Basic scripting help (FPS Tutorial) 1 Answer