• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by MC HALO · Nov 05, 2010 at 02:28 AM · movechangeswapnext

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 :)

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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

Comment
MC HALO

People who like this

1 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MC HALO · Nov 05, 2010 at 02:50 AM 0
Share

lool yea the name is crap lool

avatar image MC HALO · Nov 05, 2010 at 02:57 AM 0
Share

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 .

avatar image MC HALO · Nov 05, 2010 at 03:32 AM 0
Share

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 M 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 M and N inputs are pressed :S strange lol

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

A node in a childnode? 1 Answer

move to next scene 1 Answer

Transition from GameMaker to Unity 1 Answer

Switching Cameras at runtime 1 Answer

How to Make Melee 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges