- Home /
Enabling the right script HELP!!!
Hey Guys i have a problem that i cant solve, in my game i have decided to let the player switch between characters. I have done this perfectly and both players show up when i press the right key but the problem is when the original player returns his walk script will not enable itself. when you start the game you will have the main player if you press forward he will walk and etc. As soon as i press N it changes the player and and disables the first players move script and when i press the M key the first player will show up and the second one will go but now the first players script wont enable why is that. Here is the code that i have placed on each character:
FIRST THE MAIN PLAYER (PLAYER 1)
Script:
//drag your spawn point to here in the inspector - probably make it an empty gameobject so you can move it about on the diving board
var spawn : Transform;
//This will activate the new player
var NewPlayerPrefab : Transform;
// the following 3 lines just deactivate the meshes on the player var MainPlayer: GameObject;
var MainHair : GameObject;
var MainBody: GameObject;
var MainPlayer: GameObject;
function Update() {
if(Input.GetKey(KeyCode.N)){
// Re-spawning the new player on the current players position
Instantiate(NewPlayerPrefab,spawn.position,spawn.rotation);
//This is deactivating the move script
GetComponent(NewMoveScript).enabled = false;
MainPlayer.active = false;
MainHair.active = false; // These 3 lines deactivate the players mesh
MainBody.active = false;
} }
this script is placed on the first player as soon as i click the 2nd player shows and the following script is attached to player 2:
SCRIPT 2
//drag your spawn point to here in the inspector - probably make it an empty gameobject so you can move it about on the diving board
var spawnPlayer1 : Transform;
//This Variable will hold the first player that we start of with
var NormalPlayer : Transform;
//This line of code will activate the mesh for the first player
var MainPlayer1 : GameObject;
//This line of code will deactivate the 2nd player
var player2 : GameObject;
//This line of code will activate the Hair for the first player
var MainHairTrue: GameObject;
function Update()
{
if(Input.GetKeyDown(KeyCode.M)){
// If the M key is pressed the 2nd Player will no longer be viewable
player2.SetActiveRecursively(false);
//This line makes my first player re spawn on the second player last know position
Instantiate(NewPlayerPrefab2,spawnPlayer1.position,spawnPlayer1.rotation);
//This line of code is activating my first players mesh
MainPlayer1.SetActiveRecursively(true);
//this is activating my first players hair
MainHairTrue.SetActiveRecursively(true);
// This line of code is activating the move script on the main player
GetComponent("NewMoveScript").active = true;
}
}
Now the problem i think occurs in this script the first script turns of the move script perfectly but when this line of code is activated :
GetComponent("NewBehaviourScript").active = true;
the following message shows up:
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.SetProperty (System.Object target, System.String name, System.Object value) ResPlaye1.Update () (at Assets/ResPlaye1.js:23)
can you figure this out please thank you very much in regard :)
Answer by Loius · Nov 05, 2010 at 02:44 AM
The error is telling you that the object this script is attached to has no NewBehaviourScript component, and yet you are trying to access it.
That's kind of a terrible name for a script, by the way. xD
i just noticed that the name i put in the script was wrong lol changed it. do you mean that the code is trying to get the script from the second player if that's the case i can not add that script to the 2nd player because it has no animations and etc i created that script for the first player .
ahh the script is working perfectly the problem is that in my Prefab there is a mesh called "" NOW WHEN i press N and then Press $$anonymous$$ again it makes clones of each player and the script is enabling stuff on that not on the actual prefab. How can i stop it from making clones the code above starts to make clones once the $$anonymous$$ and N inputs are pressed :S strange lol
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to swap object again? 1 Answer
move to next scene 1 Answer
Hair Color 1 Answer
Switching Cameras at runtime 1 Answer