- Home /
changing character by clicking a button NETWORK
Hi. I have a asset manager that holds an array of characters. I also have a spawn in my game that spawns a player. Attached to the player is a "spawner" that changes the character by pressing a gui button. that spawner has this script attached to it.
#pragma strict
private var _characterMeshIndex = 0;
public var _charMeshName : String = "Srn";
private var ca : CharacterAssets;
var selectedPlayer : int = 0;
function Start ()
{
ca = GameObject.Find("CharacterAssetManager 1").GetComponent(CharacterAssets);
InstantiateCharacterMesh();
}
function OnGUI ()
{
ChangeCharacterMesh();
}
function InstantiateCharacterMesh()
{
switch(_characterMeshIndex)
{
case 1 :
_charMeshName = "Fredrik";
selectedPlayer = 2;
PlayerPrefs.SetInt("selectedPlayer", (selectedPlayer));
break;
case 2 :
_charMeshName = "Susanne";
selectedPlayer = 3;
PlayerPrefs.SetInt("selectedPlayer", (selectedPlayer));
break;
default :
_characterMeshIndex = 0;
_charMeshName = "Srn";
selectedPlayer = 1;
PlayerPrefs.SetInt("selectedPlayer", (selectedPlayer));
break;
}
if(transform.childCount > 0)
for(var cnt = 0; cnt < transform.childCount; cnt++)
Destroy(transform.GetChild(cnt).gameObject);
var mesh : GameObject = Instantiate(ca.characterMesh[_characterMeshIndex], transform.position, Quaternion.identity) as GameObject;
mesh.transform.parent = transform;
mesh.transform.rotation = transform.rotation;
}
function ChangeCharacterMesh()
{
if(GUI.Button(new Rect(Screen.width / 2 -60, Screen.height - 30, 120, 30), _charMeshName))
{
_characterMeshIndex++;
InstantiateCharacterMesh();
}
}
the problem is when i connect two players to the server. One of them controls the character selection while the other cant do anything at all.
i also have network view on the player and the "spawner".
Am i missing something?
Answer by sang · Sep 19, 2013 at 06:41 PM
If you want to instantiate an object and control it over network you could use Network.Instantiate.
If you want to call a function in both client, you could use NetworkView.RPC too.
Thanks. I went for another approach. and i'm using Network.Instantiate for that one. Thanks again for the reply!
Your answer
![](https://koobas.hobune.stream/wayback/20220613114451im_/https://answers.unity.com/themes/thub/images/avi.jpg)