- Home /
Call a function in an instanciated prefab has no effect on the prefab
I've seen similar problems and the solutions, but it seems to work for everyone but me. The Player script has an orientation parameter, which is NONE at the start. Then in want to changue it, but i can't.
GameObject playerInstance = Instantiate(player, new Vector3(spawn.x, PLAYER_HEIGTH, spawn.y), Quaternion.identity) as GameObject;
Player playerScript = playerInstance.GetComponent();
playerScript.setOrientation(directionType.NORTH);
Debug.Log(playerInstance.GetComponent().orientation);
THe Debug returns the right orientation, so, somehow, it changues it. But in game the variable is still NONE.
If what you expect is the Prefab to change, this is not going to happen. Prefabs are nothing more than serialised GameObjects at runtime.
From your code though, the instance (the one in the hierarchy view) should reflect the changes.
You never specify which type of component to get. Try using:
Player playerScript = playerInstance.GetComponent<Player>();
Debug.Log(playerInstance.GetComponent<Player>().orientation);
Still having the same problem, the Log gives me the value I set, but the object in the hierachy doesn't reflect the changues.
Does it also show the good value when you Debug.Log: playerScript.orientation
ins$$anonymous$$d of playerInstance.GetComponent<Player>().orientation
Answer by Bunny83 · Dec 15, 2016 at 01:05 PM
You gave very little information about your setup, what exactly "directionType" is (i guess it's an enum) and what exactly happens inside setOrientation. Furthermore you forget to mark your code as code so your generic parameter got interpreted as HTML tag and got stripped away.
So please take the time to read the userguide, especialle the section "Posting Questions, Answers andComments". So feel free to edit your question and re-add your code properly highlighted. You also might want to include what "setOrientation" does and what kind of thing actually should change about the object.
Next, make sure you actually inspect the right object in the hierarchy. For this i recommend to rename the instance in code so you can be sure that's the right one.
playerInstance.name = "The One";
Next thing you can do is to add a context object to your Debug.Log. That way when you click the debug message in the in the console it will "ping" the object in the hierarchy:
Debug.Log(playerInstance.name + " : " + playerScript.orientation, playerInstance);
// /|\
// |
// Context object -----/
If that still doesn't give you the right object / values, try isolating that problem by copying the relevant parts into a new / empty project and see if the problem exists there as well. If it does, feel free to add more information to your question. We only see those parts of your problem that you are willing to share. You are at the source, we don't.
Answer by Creeper_Math · Dec 17, 2016 at 11:15 PM
You were calling the instantiating a bit wrong this is what you had for instantiating
GameObject playerInstance = Instantiate(player, new Vector3(spawn.x, PLAYER_HEIGTH, spawn.y), Quaternion.identity) as GameObject;
This is what I usually find works
GameObject playerInstance = (GameObject)Instantiate(new Vector3(spawn.x, PLAYER_HEIGTH, spawn.y), Quaternion.identity);
Your answer
Follow this Question
Related Questions
how Load Prefabs after build 0 Answers
Instantiated prefab doesn't collide with player 0 Answers
How to randomly instantiate other prefabs parallel? 1 Answer
Why is instantiated animator prefabs are not working properly? 2 Answers
How to Instantiate a GameObject from a ScriptableObject piece of script? 0 Answers