x


How to change my character during the game

i have 4 character i want to change swap(switch) character during the game means when i pressed a key current character disappears & another character appear like Trine game.

how to do that its all done in scripting or something else. if this is all in scripting please show the scripting

Thanks

more ▼

asked Dec 04 '09 at 04:51 AM

Apexuni gravatar image

Apexuni
72 14 15 22

(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

Considering your 4 characters are 4 different objects in your game that have the names C1, C2, C3, C4, you will change them by pressing the Space Bar with the following code:

var selectedCharacter : int = 1;
var characterName : String;

function Update()
{
   if(Input.GetKeyDown(KeyCode.Space))
   {
      if (selectedCharacter < 4)
          selectedCharacter++;
      else
          selectedCharacter = 1;

      for (var i = 1; i < 5; ++i)
      {
         if(i != selectedCharacter)
    	 {
    		characterName = "C" + i;
    		GameObject.Find(characterName).renderer.enabled = false;
    	 }
    	 else
    	 {
    		  characterName = "C" + selectedCharacter;
    		  GameObject.Find(characterName).renderer.enabled = true;
    	 }
      }
   }
}

This script should be attached to an Empty Game Object (call it CharacterChanger).

If you select a character in your Project Hierarchy then look at the Inspector Properties you will notice in front of the character's Mesh Renderer a box with a tick inside. When you use renderer.enabled = false or renderer.enabled = true in the code, you basically check or uncheck that box, enabling or disabling your character rendering completely in the game. So, the character you want to be visible when you start the game, should have the Mesh Renderer activated (checked), while all the others should have this unchecked. After you start the game, pressing the SpaceBar will do the job.

The code above works for changing characters without changing their positions. If you move one of the characters (which I suppose you do), the next time you disable the character and enable another, the new enabled character will appear at his last position. If you want it to appear in the same position as the character before you have to copy it's transform position and maybe rotation or even scaling, depending on your game (something like GameObject.Find(characterName).transform.position = this.transform.position). I hope this is clear enough!

more ▼

answered Dec 04 '09 at 07:22 AM

ericksson gravatar image

ericksson
1.9k 11 14 45

Input.GetKeyDown(KeyCode.Space) maybe is a better input candidate since otherwise characters will change every frame as long as space is pressed. Input.GetKeyDown(..) returns true only the frame the button was pressed.

Dec 04 '09 at 07:37 AM Statement ♦♦

Also if you want your game objects to hang around, only disable the controller you're using for that game object instead of disabling the whole game object. GameObject.Find(characterName).GetComponent(typeof(MyControllerType)) if you get my drift.

Dec 04 '09 at 07:39 AM Statement ♦♦

Very good comments. Thanks a lot! Now it should be fairly easy for everyone to interchange characters.

Dec 04 '09 at 09:22 AM ericksson

I edited the answer to include the Input.GetKeyDown suggestion for people who don't read the comments.

Dec 04 '09 at 09:57 AM ericksson

U mean this code add to all character & i change your code just replace the enabled & add active its work. but during the game when i pressed space current character deactivated but second character not activated. ????

Dec 05 '09 at 10:09 AM Apexuni
(comments are locked)
10|3000 characters needed characters left

thanks Ericksson

i try to add this code in my Object (i adding this code to my character) but there is problem when compling this code. Error: Assets/NewBehaviourScript.js(15,38): BCE0019: 'enabled' is not a member of 'UnityEngine.GameObject'

more ▼

answered Dec 05 '09 at 10:58 AM

Apexuni gravatar image

Apexuni
72 14 15 22

Hi, Amol, sorry about that. I written the initial code just for giving you a general idea but I didn't test it myself. Now I have created a project and I tested that and I modified the script in my initial post to reflect the changes. It should work without a problem now.

Dec 05 '09 at 12:29 PM ericksson

thanks Ericksson its work properly

Dec 08 '09 at 01:42 PM Apexuni
(comments are locked)
10|3000 characters needed characters left

Hi I'm tried this script and came up with this error:

MissingComponentException: There is no 'Renderer' attached to the "C1" game object, but a script is trying to access it. You probably need to add a Renderer to the game object "C1". Or your script needs to check if the component is attached before using it. CharactorSwap.Update () (at Assets/CharactorSwap.js:18)

What do I need to do to fix this?

more ▼

answered Mar 03 '10 at 10:33 PM

user-1484 (google) gravatar image

user-1484 (google)
-4

Please don't post questions as answers...!! Consider adding a comment under a particular answer that you were trying to use...!

Mar 03 '10 at 11:51 PM Lipis
(comments are locked)
10|3000 characters needed characters left

Hi I have a "NullReferenceException CharacterChanger.Update () " each time I pass GameObject.Find(characterName).renderer.enabled = true; any idea?? thanks

more ▼

answered Feb 23 '10 at 01:28 PM

sdl gravatar image

sdl
-4

This a comment, not an answer... Look for the 'add comment' text link next time.

Feb 23 '10 at 02:17 PM Motionreactor
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5272
x1081

asked: Dec 04 '09 at 04:51 AM

Seen: 4653 times

Last Updated: Mar 03 '10 at 11:52 PM