|
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
(comments are locked)
|
|
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:
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! 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)
|
|
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' 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)
|
|
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? 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)
|
|
Hi I have a "NullReferenceException CharacterChanger.Update () " each time I pass GameObject.Find(characterName).renderer.enabled = true; any idea?? thanks 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)
|
