- Home /
Player prefab instantiation
Hello,
I want to instantiate a different game object for each player, so I made 2 prefabs in the resources folder (redPlayer & bluePlayer). In my GameManager, I declared two public gameObjects, RedPlayer and BluePlayer and assigned the prefabs from the resources folder to those GameObject on the script.
An I have made another GameObject for the player prefab. here is the code
if (PlayerManager.LocalPlayerInstance == null)
{
if (GameObject.Find("redPlayer"))
{
playerPrefab = BluePlayer;
PhotonNetwork.Instantiate(this.playerPrefab.name, new Vector3(0f, 5f, 0f), Quaternion.identity, 0);
}
else
{
playerPrefab = RedPlayer;
PhotonNetwork.Instantiate(this.playerPrefab.name, new Vector3(0f, 5f, 0f), Quaternion.identity, 0);
}
}
However, when playing, I get an error message that the playerPrefab wasn't assigned.
suggest some sanity checks e.g.:
Debug.Log("BluePlayer prefab: " + BluePlayer.name);
Debug.Log("RedPlayer prefab: " + RedPlayer.name);
Not familiar with PhotonNetwork, but it looks like you are just passing the name of the prefab to that Instantiate function, rather than a reference to the prefab. Perhaps there is another version of that function? (similar to this version of Object.Instantiate
- https://docs.unity3d.com/ScriptReference/Object.Instantiate.html)
Thanks for yout time. Actually photon requires a string and not a GameObject, that's why I made the .name at the end, either way it will show an error
Ah, I see now, it loads the prefab separately on each system, so they wouldn't have the same reference. I assume the prefabs exist on both systems, in the root of the resources folder, with the names displayed by the sanity checks? Did it pass the sanity checks too?
Answer by JusSumGuy · Oct 07, 2018 at 08:55 PM
Im not sure but it seems like your code doesn't handle a situation where both the blue and the red players can not be found. Because if red is found then blue is instantiated. The else says if red can't be found then instantiate red. But what if both both object cant be found? with this code only red would instantiate. Unless, you have another script to call the logic twice. Like i said im not sure, don't attack me if im wrong lol.,I'm not sure but it seems like your saying if the red player is defined then instantiate the blue player. then with the else your saying if the red player is not defined instantiate the red player. Are you sure you didn't get your logic in reverse? if you didn't it might work if you check for blueplayer and do what you did for your redplayer in the second if statement. My point is what if the blueplayer is defined your code should work. But your code doesn't handle the variant where both player objects can not be found.
Seems like that would throw an error like that while playing. If you are getting both a red and blue player at times, then just a red player with no blue, I think that's the issue.
Hello, thanks for your answer, yeah you are right, the players both get a red player prefab. what I wanted to say with my code is if a player joins, and there is already a player in the game who have redPlayer prefab, then give him the blueplayer, else if he is the first one, we instantiate the redPlayer for him, but well I have an error in my logic.. any ideas though?
Is the RedPlayer tag "redPlayer"? Because if both players get the redPlayer prefab, I think you just need to change GameObject.FindGameObjectWithTag("redPlayer"); if that's the tag. but if your looking for the Object itself then I think you just need capitilize the r in red. GameObject.Find("RedPlayer");
Your answer
Follow this Question
Related Questions
Instantiate bullet towards Player position 1 Answer
Instantiating, methods and returning from a seperate script. 1 Answer
Why object created by Instantiate doesn't keep data in attached script 0 Answers
How do I make the platform spawn after the other one 1 Answer
Bullet wont collide for the other player (PHOTON+UNITY) 1 Answer