- Home /
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!
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?
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.
@robertbu The game object is active (shown in the hierarchy).
@b_chrome $$anonymous$$y code is exactly the same $$anonymous$$us the name of the object and the silly typo I had :P
So are you getting Update() calls? If so what is the value of 'flag' at that point?
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.
Your answer
Follow this Question
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