Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Wutai · Aug 13, 2013 at 09:39 AM · updateassign

Help with assigning an object with Instantiate

I want to be able to call SetItem() from another script, and give it a GameObject. Then assign it to the GameObject myOb, and check it in Update().

It instantiates a new myOb ok, but is always null in Update(). What am I doing wrong?

 private GameObject myOb;
 
 // Use this for initialization
 void Start () 
 {
 }
 
 // Update is called once per frame
 void Update () 
 {        
     if(myOb != null)//currently always null
     {
         Debug.Log("Selected object is: " + myOb);
     }
 }
 
 public void SetItem(GameObject newOb)
 {
     myOb = ((GameObject)Instantiate(newOb));
     //myOb = newOb;
     Debug.Log("New Object is: " + myOb);//prints "New Object is: Tree(Clone) (UnityEngine.GameObject)"
 }
Comment
Add comment · Show 4
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 Linus · Aug 13, 2013 at 10:19 AM 0
Share

Does making myOb public ins$$anonymous$$d of private change anything?

avatar image verenion · Aug 13, 2013 at 10:20 AM 0
Share

This looks like it should work. You don't need the extra set of brackets here though:

 myOb = (GameObject)Instantiate(newOb);

Is this class a mono-behaviour? If not, are you calling the correct instance, is the update function being called at all? What does the following code do in place of the update function?

 void Update () 
 {     
     
     Debug.Log("Selected object is: " + myOb);
     
 }

I think this could be an instance problem, is the instance you are calling SetItem() on the same instance you are running Update() on? Can you show the code that calls SetItem()?

avatar image Wutai · Aug 14, 2013 at 12:18 AM 0
Share

Thanks verenion, looks like it was an instance problem. Its fixed now, cheers guys.

avatar image verenion · Aug 14, 2013 at 09:30 AM 0
Share

Cool! Remember to upvote comments/ answers

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Xtro · Aug 13, 2013 at 02:02 PM

Make sure your class inherits MonoBehaviour and it's attached to a gameobject in the scene.

You can't declare an Update function to a regular class (non-MonoBehaviour) and expect it to be called in the game update loop. It has to be a MonoBehaviour and be attached to a gameobject.

Comment
Add comment · Show 2 · 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 verenion · Aug 13, 2013 at 04:47 PM 0
Share

I said this 6 hours ago. This isn't an answer to the question anyway, not until we know exactly what he/she wants.

avatar image Xtro · Aug 13, 2013 at 04:58 PM 0
Share

Oh sorry. I didn't read your comment because it wasn't an answer. So, I had my answer in my $$anonymous$$d and I wrote it as an answer. Now I look at your comment and it has few questions in it. $$anonymous$$ine was a statement which is more answerish than a question.

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Create Texture2D and assign image to it through a script 1 Answer

Cannot assign script to objects when class is within a Namespace 1 Answer

Where are the built scripts stored in a build? 1 Answer

Assigning a gameObject in the script 3 Answers

Scripts won't automatically update, always need to 'reset' them in the inspector for update 11 Answers


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