spawn another object and not primitive in appwarp unity sample

0 votes
hey i was just working around with the appwarp sample and used the appwarp.cs script to instantiate a capsule and move it around.but now i want to use it for my game in which i have a preftab for a player and not the primitve object.so i nedd to instantiate the player at runtime instead of the cube.i tried using the player preftab without static and it keeps instantiating many of its clones,whereas when i use primitive ones it spawns only one.i need to instantiate the player object but since obj is static i cannot assign a player.and removing static keyword makes it run again and again .plz help!
asked Jun 30, 2015 in AppWarp by Mihir Solanki (27 points)

1 Answer

0 votes

Hi Mihir,

I suppose you are using this sample. You can't use 'GameObject.CreatePrimitive' for instantiating your gameObject, which is not of primitive type. You have to use 'GameObject.Instantiate' instead, for instantiating your player as follows:

public GameObject playerPrefab;
private static GameObject myPlayer;
void Start(){
     ///code......... 
     myPlayer = playerPrefab;
     addPlayer();
}
public static void addPlayer()
{
     //you have to change this piece in this method.
     obj = Instantiate (myPlayer); 
     obj.transform.position = new Vector3(732f,1.5f,500f);
}
For more info on instantiating different player object, please check our VikingDemo project. 
Let us know if you face any problem.

 

answered Jul 1, 2015 by sumit.balodi (40 points)
k will try
though i am doubtfull as i hav tried this type of meathod and have seen my player instantiating many copies
no this method does not work as it is continuously instantiating the player preftab's
clones instead of just one.please help!
ok i did a check  if(obj==null) which instantiates the object only once other than the player on the network so its ok but i get this weird thing when the player instantiates its position gets changed suddenly and it moves towards the end of the map.i guess this is happening due to the function addplayer which gets its message form the listener script and this changes its position .how do i make sure the object stays at hte same positon and inherits the values given correctly .thanks
The code should work, as the 'addPlayer()' method is only called once. Please re-check your project, that 'Instantiate' is called single time only, not in a loop.
 
The Instantiated player actually moves to Vector3.zero position, when no player has joined the room except you, it's position is changing in 'Update'. That's why it seems like, it is moving towards the end of the map in the 'Scene' view.

It will change its position when the new user joins the room, and update itself accordingly. The default position is fixed at start from this line:

public static Vector3 newPos = new Vector3(0,0,0);

This demo is made up for testing of maximum 2 players. Please also check the VikingDemo (https://github.com/SuyashMShepHertz/UnityAppWarpVikingDemo)  sample project where you can have multiple player in a room.
If you have any queries, please feel free to ask us.
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...