How can I modify/change a prefab?
In my game I have a ball (The player) with i want to make skins to. At the moment I'm able to change the material of my prefab player. and the next step is to give the player a hat. I have done research but I'm not able to find the answer!
My player is made out of a GameObject called Player that is the parent to 4 other GameObjects. The GameObject Player has a Sphere Collider, a Rigidbody and my Player Movement script.
The 4 child GameObjects of my Player GameObject is:
Trail (Not used yet)
Color (Switching between colors via materials)
Face (Not used yet)
Hat (My Problem)
I'm trying to modify my Player prefab so that the user can select a hat that then will go via script and connect a hat prefab and child it under the Hat GameObejct.
My Player Prefab + My TopHat Prefab = Player with TopHat Prefab
The way is want it to work (which i hope is possible) is that using my SkinsMenu. you can choose from an array of pictures (which works) and then you click the button "Choose Hat" then the code should do something like this:
Picture = number
number = hat
give the player hat[number]
If there are missing some information just tell me and I will find and post it!
Thanks for all the help I can get!
Sorry if this is a duplicate (I tried reseaching alot!)
- Michael
(First time posting, hope I done it right)
Answer by SRohde · Nov 01, 2017 at 11:47 AM
I am not sure if changing the whole prefab is actually what you want/need.
If I understood correctly you just want the gameobject/prefab to change according to the selections made within the skin menu, no?
Shouldn't it suffice to load a default prefab and simply exchanging the associated hat-model with whatever was chosen within the skin menu in runtime? You'd just need to save the index of the hat-image chosen into a variable and use that index within a script (maybe attached to an object within the prefab) to load the selected hat from your resources.
If I misunderstood anything, just let me know, but from your description this seems like a simple way to achieve what you want.
Hello SRohde!
Thanks for your anwser. I think you are on the right track. But I'm having some issues trying to code was you are telling me... Can you try to show me some code of it?
I have a Level$$anonymous$$anager script, a Player Script and a SkinChooser script. Level$$anonymous$$anager script should instantiate/spawn the player with the right skin (the hat) The Player script is just there to control the player. SkinChooser script is connectet to my skinsmenu so that should handle the input from the picture above.
I can show you what i have done. But my code is really messy for time being. This would be the parts of my code which should handle the skins and spawning the player.
Level$$anonymous$$anager:
public GameObject PlayerPrefab;
public Vector3 SpawnPoint;
public Vector3 SpawnRotation;
public Quaternion SR;
void Start () {
SR = Quaternion.Euler (SpawnRotation);
Instantiate (PlayerPrefab, SpawnPoint, SR);
int hn = SkinChooser.hatNumber;
Transform Hat = FindObjectOfType<Player> ().transform.Find ("Hat");
Instantiate (SkinChooser.hats [hn], Hat);
}
SkinChooser:
// The Hat
public static GameObject[] hats;
public Sprite[] hatImages;
public Image currentHat;
public int hatImageNumber = 0;
public static int hatNumber = 0;
public void ChangeHatImage (int n) {
if (n == 1) {
if (hatImageNumber + 1 < hatImages.Length) {
hatImageNumber++;
}
} else if (n == -1) {
if (hatImageNumber - 1 > -1) {
hatImageNumber--;
}
}
}
public void ChooseHat () {
hatNumber = hatImageNumber;
}
I'm still new on coding and Unity. So my knowledge ins't that great for now :) I hope you can help me with my problem! - $$anonymous$$ichael
Hello SRohde! While trying over and over agian. I tried something which did work! First i made my SkinChooser a singleton. Then did this in my Level$$anonymous$$anager script:
void Start () {
SR = Quaternion.Euler (SpawnRotation);
GameObject player = Instantiate (PlayerPrefab, SpawnPoint, SR);
Transform hat = player.transform.GetChild (3);
int hn = SkinChooser.skinChooser.hatNumber;
GameObject ourHat = Instantiate (SkinChooser.skinChooser.hats [hn]);
ourHat.transform.SetParent (hat.transform);
ourHat.transform.localPosition = new Vector3 (0, 0, 0);
}
Thanks for your help. I'm so happy now that i figured it out! If anyone else have problems with this. Just write and I will help you! $$anonymous$$ichael
Hi sorry, I did not check for new messages for a while. Glad you managed to find a solution on your own ^^ Looks good to me :) Best regards!
Your answer
Follow this Question
Related Questions
Instantiate a script into an instantiated prefab 3 Answers
How to instantiate Prefabs for an endless game? 0 Answers
Method not working inside FixedUpdate 2 Answers
Prefab script member values not correct 0 Answers