• 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
0
Question by MMDCLXVI · Mar 04, 2013 at 08:39 PM · playerrespawn

Player Respawn Script - Not Working

Hello to all, I was wondering if anyone out there on the this wide world of 8 billion people could lend me just a moment of there time and help me out with some scripting problems I've being having.

Know firstly before I start I've being using the simple health system that I obtain on the Unity Asset store (Grid Digital - Simple Health System) to create a health system. Know I am aware that the way the script works is that when the enemy hits the player a heart will disappear on the player GUI.

In case you's are not aware of what this script looks like, it is as followed....

 var OneHeart : Texture2D; //These variables contain the textures used for the gui.
 var TwoHearts : Texture2D;
 var ThreeHearts : Texture2D;
 var FourHearts : Texture2D;
 var NoHearts : Texture2D;
 
 static var Hearts : int = 4; //This is the amount of hearts the character has.
 
 var startPosition : Transform;
 
 function Update (){
     if(Hearts == 4){ //These if statements change the Gui Texture based on the static var Hearts.
         guiTexture.texture=FourHearts;
     }
     if(Hearts == 3){
         guiTexture.texture=ThreeHearts;
     }
     if(Hearts == 2){
         guiTexture.texture=TwoHearts;
     }
     if(Hearts == 1){
         guiTexture.texture=OneHeart;
     }
     if(Hearts == 0){
         guiTexture.texture=NoHearts;
     }
     if(Hearts > 4){ //These two if statements keep the static var Hearts within the 0-4 range.
         Hearts = 4;
     }
     if(Hearts < 0){
         Hearts = 0;
         
         //CompareTag ("Player").transform.position = startPosition;
         //guiTexture.texture = FourHearts;
     }
 }

Know I've writing a simple respawn script that uses the above script, it is as follows....

 var OneHeart : Texture2D; //These variables contain the textures used for the gui.
 var TwoHearts : Texture2D;
 var ThreeHearts : Texture2D;
 var FourHearts : Texture2D;
 var NoHearts : Texture2D;
 
 static var Hearts : int = 4; //This is the amount of hearts the character has.
 
 var startPosition : Transform;
 
 function OnTriggerEnter(theCollider : Collider) 
 {
     if(Hearts < 0){
         Hearts = 0;
         
         CompareTag ("Player").transform.position = startPosition;
         guiTexture.texture = FourHearts;
     }
 }

Furthermore the script seems to work, in the sense that there is no errors, however the player won't respawn back at his original starting position. I was wondering if someone could look over the script and tell me if there was something wrong with it, any and all help would be great.

Go dtí an chéad uair eile, slán leat

Comment
Add comment
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
0

Answer by PAEvenson · Mar 04, 2013 at 09:21 PM

Well, first I dont see you storing the startPosition anywhere. You most likely want to do this in the Start or Awake functions:

 startPosition = gameObject.transform;

If that isnt the case my next guess would be with OnTriggerEnter function. To have OnTriggerEnter actaully trigger you need atleast one of the colliders needs to have a rigidbody attached to it AND one of the colliders needs the IsTrigger toggle set in the inspector.

Comment
Add comment · 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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems with a player spawning and handling script 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

how to spawn and destroy 2 Answers

Moving your player to another location onTrigger 0 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges