I’m following the Brackey’s 2D platformer tutorial, (his forums won’t validate my email address so I can’t post there), but my script won’t delete my player object when he falls off the screen.
Here’s my player script. Line 15-18 is where it doesn’t seem to be doing calling DamagePlayer()
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
[System.Serializable]
public class PlayerStats {
public int Health = 100;
}
public PlayerStats playerStats = new PlayerStats();
public int fallBoundary = -20;
void update(){
if (transform.position.y <= fallBoundary)
DamagePlayer (9999999);
}
public void DamagePlayer(int Damage) {
playerStats.Health -= Damage;
if (playerStats.Health <= 0) {
GameMaster.KillPlayer(this);
}
}
}
Here’s the GameMaster Script that kills the player:
using UnityEngine;
using System.Collections;
public class GameMaster : MonoBehaviour {
public static void KillPlayer (Player player) {
Destroy (Player.gameObject);
}
}
I put the lower case ‘p’ but it made no difference.
I’m pretty new to all of this, and this is my first attempt at scripting (I’m just typing what the video tells me, and learning what each line does) but where would I put that in on the script? within the update part? IT doesn’t like the word function anywhere, and when I put it within the update script, it doesn’t like the ‘}’ Oh man, I’m pretty lost if I cant even add a damn line of code
Functions can’t go inside other functions, so you can’t put OnBecameInvisible() inside Update(). You put OnBecameInvisible() outside of Update() as it’s a function just like Update(). The function will only run when the object its attached to goes outside the view of the camera. So put the function in your Player script outside of Update() with Destroy(gameObject); inside of it
“Expression denotes a ‘type’ , where a ‘variable’, or ‘value’ or ‘method group’ was expected.”
The best overload method match for UnityEngine.Object.Destroy(UnityEngine.Object)’ has some invalid arguments
and
Argument ‘#1’ cannot convert ‘object’ expression to type ‘UnityEngine.Object’
I’m a frustrated failure… are there any easier platforming tutorials that I could start with that anyone suggests? I can program, and made a few good games in C2 (I know, visual programming ) but I can’t seem to really get all the tiny things that can make the code blow up.
player is the object (which you would destroy), of class Player
GameObject is the class
gameObject is the object
Pay attention to the casing used, as its important.
Destroy(gameObject) will work.
Destroy(player.gameObject) should also work.
The following will never work as they are referencing classes rather than the objects themselves.
Destroy(GameObject)
Destory(Player.GameObject)
Destrory(Player.gameObject)
Destroy(player.GameObject)
that worked! Thanks guys! But how did that work as gameObject was never defined anywhere. I didn’t think that the code would know that gameObject was tied to either GameObject or Player. I know it’s case sensitive, but I figured if I changed the case of something that was already defined, it wouldn’t know what to match it up with.
and why would the other code not work? That way I don’t make these mistakes again
I don’t properly know myself, I’m pretty sure what James said above is what you’re looking for. GameObject is the base class for all entities in Unity scenes. gameObject is the actual object though? I think. Don’t quote me on it.