Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 apple741 1 · Sep 14, 2011 at 09:35 AM · javascriptios

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);
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

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 apple741 1 · Sep 14, 2011 at 10:13 AM 0
Share

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() ?

avatar image syclamoth · Sep 14, 2011 at 10:21 AM 0
Share

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.

avatar image apple741 1 · Sep 14, 2011 at 10:36 AM 0
Share

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

avatar image syclamoth · Sep 14, 2011 at 10:41 AM 0
Share

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.

avatar image syclamoth · Sep 14, 2011 at 10:44 AM 0
Share

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.

Show more comments
avatar image
0

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();

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

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


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