- Home /
Why is my code not working?
I am coding in Javascript. This code doesn't have any errors... but it doesn't work. The only thing that shows in the log is 'The player committed suicide' every time I hit space. Edit: I don't expect the collision part to work because my player controller does not have a rigid body.
#pragma strict
function Start () {
}
function Update () {
if(Input.GetButtonUp("Jump")){
VoluntaryDeath();
}
}
var dead = false;
function OnCollisionEnter (theCollision : Collision){
if(theCollision.gameObject.name == "Crazy Cube"){
Debug.Log("The player was hit by the cube.");
dead = true;
}
}
var thePrefab : GameObject;
function SpawnCorpse(){
var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
Debug.Log("A corpse was spawned.");
}
function VoluntaryDeath(){
Debug.Log("The player committed suicide.");
dead = true;
}
if(dead == true){
Debug.Log("Dead is now true.");
SpawnCorpse();
Destroy(gameObject, 5);
}
Answer by meat5000 · Aug 31, 2013 at 11:42 AM
You press space and this function is called
function VoluntaryDeath()
{
Debug.Log("The player committed suicide.");
dead = true;
}
This sets dead to true and there is nowhere that sets it back to false.
Constant death.
I'm not really sure what you are trying to do, but take a look at this re-hashed code and play with it as you please.
#pragma strict
var dead = false;
var thePrefab : GameObject;
function Start () {
}
function Update ()
{
if(Input.GetButtonUp("Jump"))
{
VoluntaryDeath();
}
if(dead == true)
{
SpawnCorpse();
Destroy(gameObject, 5);
Debug.Log("The player committed suicide.");
Dead = false;
}
}//END OF UPDATE()
function SpawnCorpse()
{
var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
Debug.Log("A corpse was spawned.");
}
function VoluntaryDeath()
{
Debug.Log("Dead is now true.");
dead = true;
}
function OnCollisionEnter (theCollision : Collision)
{
if(theCollision.gameObject.name == "Crazy Cube")
{
Debug.Log("The player was hit by the cube.");
dead = true;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Is it possible to destroy an object at a specific position? 2 Answers
Damage/Healing over time 1 Answer
Int and Javascript help 2 Answers
Boxcollider 2D Destroyed 2 Answers