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 S0rn0 · Mar 09, 2014 at 10:39 PM · gameobjectprefab

Calling script on prefab using array of prefabs.

I am trying to call a method of a class attached to a prefab that I have instantiated with the reference to the GameObject stored in an array.

What is happening is, I instantiate an instance of prefab Y, then store there references into a 2d array like so:

 GameObject object = (GameObject) Instantiate(Y, someVec3, Quaternion.Identity);
 object.name = "Name";
 array[0,0] = object; // 2d array of GameObjects

Then I attempt to call a method from a script attached to the game object like this:

 array[0,0].GetComponent<ScriptName>().TheMethod();

In my project, calling the method should render the game objects mesh by simply setting a boolean=true, then the scripts Update() method deals with it.

This is not occuring. If I set the boolean=true in the scripts Start() method and instantiate the prefab it works fine. I used a Debug.Log() call to see if the method is actually being called using the above code, and it is and indeed this boolean is being set to True. Yet nothing happens after that.

The update method is as follows:

 void Update() {
 
      if (flag) {
           // Render
      }
 }

and the TheMethod() is as follow:

 public void TheMethod() { flag = true; }

where flag has been declared as a private bool.

I am baffled as to why this is occuring. Thank you in advance for any advice/solutions!

Comment
Add comment · Show 7
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 b_chrome · Mar 09, 2014 at 10:59 PM 0
Share

I guess that this isn't really your code as you can't have variables called "object" and array syntax uses [ and ].

Can you post your actual code?

avatar image robertbu · Mar 09, 2014 at 11:31 PM 0
Share

Note if either your script or the game object the script is attached to is not enabled, then you will not get Update() calls. If this is the issue, you can enable things when you call 'The$$anonymous$$ethod()' method.

avatar image S0rn0 · Mar 09, 2014 at 11:43 PM 0
Share

@robertbu The game object is active (shown in the hierarchy).

avatar image S0rn0 · Mar 09, 2014 at 11:45 PM 0
Share

@b_chrome $$anonymous$$y code is exactly the same $$anonymous$$us the name of the object and the silly typo I had :P

avatar image robertbu · Mar 09, 2014 at 11:54 PM 0
Share

So are you getting Update() calls? If so what is the value of 'flag' at that point?

Show more comments

1 Reply

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

Answer by S0rn0 · Mar 10, 2014 at 05:35 AM

I ended up solving the problem. I assumed Start() would be called immediately before I had a chance to call the method.

What happened was, my method was called THEN start was called. Within start I was initializing the flag to false which over wrote the method calls change.

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

21 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 avatar image avatar image avatar image

Related Questions

Prints correct on console but not updating on Prefab/Gameobject. 2 Answers

Is an object created when dragging a prefab to a GameObject script field 1 Answer

How to create and save a gameobject to a prefab with a script 3 Answers

Switch the prefab of a game object from a script 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 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