• 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 diegodm223 · Dec 14, 2016 at 08:29 PM · c#instantiateprefabprefabsparameters

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.

Comment
Add comment · Show 5
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 UnityCoach · Dec 15, 2016 at 09:47 AM 1
Share

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.

avatar image Cambesa · Dec 15, 2016 at 10:01 AM 1
Share

You never specify which type of component to get. Try using:

 Player playerScript = playerInstance.GetComponent<Player>();
 Debug.Log(playerInstance.GetComponent<Player>().orientation);

avatar image UnityCoach Cambesa · Dec 15, 2016 at 10:07 AM 0
Share

Oh! Good catch!

avatar image diegodm223 Cambesa · Dec 15, 2016 at 10:27 AM 0
Share

Still having the same problem, the Log gives me the value I set, but the object in the hierachy doesn't reflect the changues.

avatar image Cambesa diegodm223 · Dec 15, 2016 at 11:11 AM 0
Share

Does it also show the good value when you Debug.Log: playerScript.orientation ins$$anonymous$$d of playerInstance.GetComponent<Player>().orientation

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

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

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


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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

12 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

Related Questions

how Load Prefabs after build 0 Answers

Instantiated prefab doesn't collide with player 0 Answers

How do I instantiate certain objects to appear in a specific spot? 3 Answers

how to call the animation of prefab 0 Answers

Issue Instantiating prefab in C# 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges