Been stuck for days now with two mind numbing issues. First off i’m using a ‘KillZone’ script that’s attached to my ‘Enemy’ GameObjects in my scene. Whenever my ‘Player’ GameObject will collide with my ‘Enemy’ GameObject in the scene, my ‘Player’ will be destroyed ala Super Mario style
Problem No.1 in my ‘KillZone’ Start(), the ‘player’, ‘bigBall’, ‘gameController’ GameObjects are available in the log however in the OnTriggerEnter2D(),whenever i run into the ‘Enemy’ GameObject in the scene the log says ‘player’ ‘bigBall’ are Not available . ‘player’ ‘bigBall’ are both prefabs dropped into the scene manually and not spawned using a script.
Problem No. 2 Whenever my ‘player’ GameObject collides with the ‘Enemy’ GameObject i just passes through and i receive the logs but my ‘player’ Game Object doesn’t get destroyed.
What am i doing wrong? Hope you guys can assist me with this…
using UnityEngine;
using System.Collections;
public class KillZone : MonoBehaviour
{
private GameController gameController;
private GameObject player;
private GameObject bigBall;
public bool isPlayerDead = false;
public bool isBigBallDestroyed = false;
void Start()
{
player = GameObject.FindWithTag("Player");
if (player != null)
{
Debug.Log ("Player is available in Start() ");
}
else
Debug.Log ("Player is not available in Start() ");
bigBall = GameObject.FindWithTag("BigBall");
if (bigBall != null)
{
Debug.Log ("BigBall is available in Start() ");
}
else
Debug.Log ("BigBall is not available in Start() ");
GameObject gameControllerObject = new GameObject ();
gameControllerObject = GameObject.Find ("GameManager");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent<GameController>();
Debug.Log (" 'GameController' scripts was found! ");
}
if (gameController == null)
{
Debug.Log ("Cant find any 'GameController' scripts! ");
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (player != null)
{
Debug.Log ("Player is available in 'Player' if statement");
if (gameController != null)
{
Debug.Log ("GameController is available in 'Player' if statement");
Debug.Log ("Game Over. Player has been destroyed");
isPlayerDead = true;
Destroy(other);
return;
}
else
Debug.Log("GameController was NOT available in 'Player' if statement");
}
else
Debug.Log ("Player is NOT available in 'Player' if statement");
if (bigBall != null)
{
Debug.Log ("BigBall is available in 'BigBall' in statement");
if(gameController != null)
{
Debug.Log ("GameController is available in 'BigBall' if statement");
Debug.Log ("Game Over. BigBall has been destroyed");
isBigBallDestroyed = true;
Destroy (other);
return;
}
else
Debug.Log("GameController was NOT available in 'BigBall' if statement");
}
else
Debug.Log ("BigBall is NOT available in 'BigBall' if statement");
}
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
//private bool fall = false;
public bool stomp = false;
public GameObject[] enemies;
float F;
void Start()
{
F = gameObject.GetComponent<MoveScript>().step;
enemies = GameObject.FindGameObjectsWithTag("Enemy");
}
void Update()
{
if (stomp)
{
Debug.Log ("Stomp boolean is TRUE");
Vector2 Y = GameObject.Find("Enemy").transform.localScale;
Y.y = 0.5f;
GameObject.Find ("Enemy").transform.localScale = Y;
Vector2 tempY = GameObject.Find ("Enemy").transform.position;
tempY.y -=0.1f;
GameObject.Find ("Enemy").transform.position = tempY;
F = 0.0f;
gameObject.GetComponent<MoveScript>().step = F;
Invoke("EnemyDeath", 2f);
}
}
void EnemyDeath()
{
if (gameObject.tag == "Enemy")
{
Destroy (gameObject);
Debug.Log ("Enemy will be destroyed");
} else
Debug.Log ("Enemy could not be destroyed");
}
}
Big thank you for the reply, that worked
Do you have an idea why i’m running through my ‘Enemy’ GameObjects in the scene and i’m only receiving a log? The ‘Player’ doesnt get destroyed even after i have updated the scripted with your recommendation above.
I could set that up in a test bed, but wont get to that for hours.
Oh, OnTriggerEnter may, or may not work using CharacterControllers, OnTriggerEnter is generally a physics controlled thing. Incremental movement is not well supported.
Consider using OverlapSphere if you find a problem with it.
Just realized now when i zoomed the Scene View all the way out, while i was playing. One enemy in the far distance that gets Destroyed for some reason, when i jump onto another enemy.
As i have made a prefab of my ‘Enemy’ GameObject (script above) and i have placed multiple ‘Enemy’ GameObjects manually in the scene. Do you guys have any suggestions how to keep track of each of the enemies?
Now when i jump and kill one of the prefab ‘Enemy’ GameObjects in the scene sometimes another enemy gets destroyed in addition to or instead of the one i jumped onto. This is a bit confusing. Any suggestions would be appreciated !!! Thanx in advance.
Your Enemy class is confusing. Could you elaborate on what you want that class to do?
The name seems to indicate that this an enemy, yet you have an array of enemies. Is this class tracking all enemies or just one?
In the Update method you do a bunch of GameObject.Find(“Enemy”). Each of these calls will find just one GameObject named “Enemy”; I’m assuming they would each return the same game object, but I wouldn’t rely on that. You do some transformations on the game object returned from each call then Invoke “EnemyDeath” which destroys THIS game object if THIS game object’s tag is “Enemy”. Is that really what you want?
As an aside, GameObject.Find is slow, you shouldn’t do it in Update if at all possible, and you certainly shouldn’t do 4 of them. If you have to do the Find, then cache the returned game object and use that.
The Enemy Script above i adapted from a Java Tutorial on Youtube on how to create a enemy that basically drops through the floor once the player jumps on it. The script seems to be working fine apart from sometimes another Enemy GameObjects gets destroyed when i kill one.
Been programming for 3 months now so its a steap learning curve for me. All i need is just a bit of direction on how im suppose to go about managing the enemies in my scene. I believe the error im getting is based on what you have mentioned above. The Array above i added as i though it might help solve the issue im facing, i have no function to track the enemies yet …
As im not spawing any enemies, do i need to use some Array to call them whenever my Player kills one? As all of them use only one prefab.
Would it be better to create mutiple Enemy GameObject prefabs and name them differently, to place them manually in the scene to ensure i have the correct reference to each one that is killed?
Any general tips would be appreciated…Thanx again for taking the time to reply.
Ok, lets break this down into smaller and more focused pieces.
You have an Enemy. That enemy can be jumped on (stomped). When the enemy is stomped, it drops through the floor. The enemy doesn’t care about any other enemies.
Let’s work on this part first. Scanning through your Enemy class, we have some things to clean up.
An enemy doesn’t care about any other enemies, so lets remove the enemies array.
There is a MoveScript attached to the same GameObject with which we’ll interact. So we should grab a reference to that.
Since we’re depending on having a MoveScript attached, we can tell Unity that we require this component.
We’re holding onto some “step” value from the MoveScript, but we’re not using it. It looks like we’re just using it to trigger the MoveScript to stop moving?? Perhaps instead of delving into the MoveScript and setting some step value, we should just message the MoveScript that we’re dead.
We’re modifying this enemy’s transform when its stomped so we can hold a reference (cache locally) to the transform. There’s some debate on if this necessary, but it won’t hurt and I’m in the habit of making a _transform reference.
Since this is the Enemy class, we can call our death method simply “Death” or “Die”. And as we know this GameObject is an enemy since this is the Enemy class, we don’t need to check the tag.
We only want to invoke Death once.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(MoveScript))]
public class Enemy : MonoBehaviour {
public bool stomp = false;
Transform _transform; //cache transform locally
MoveScript moveScript; //reference to MoveScript
bool isDead = false;
void Start()
{
moveScript = GetComponent<MoveScript>();
_transform = transform;
}
void Update()
{
if (stomp)
{
Debug.Log ("Stomp boolean is TRUE");
Vector2 Y = _transform.localScale;
Y.y = 0.5f;
_transform.localScale = Y;
Vector2 tempY = _transform.position;
tempY.y -=0.1f;
_transform.position = tempY;
if (!isDead)
{
isDead = true;
moveScript.StopMoving();
Invoke("Death", 2f);
}
}
}
void Death()
{
Destroy (gameObject);
}
}
I’m not modifying your transform Update code. But I will suggest you looking at the Lerp function (and examples).
We’ll have to modify the MoveScript to have a public StopMoving method that handles whatever MoveScript needs to do to stop moving. That’s beyond the scope of this post
The second thing you wanted was some way of tracking the Enemies. For that you need another class, say EnemyTracker or EnemyManager on a separate GameObject. What that GameObject should do is hold a list of Enemy instances. You don’t say why you want to track the Enemy instances. So I think you should take a shot at writing the class to do what you want.
Just a couple of things to remember:
If you have a list of Enemy objects and your Enemy class calls Destroy, your list will now have a reference to a destroyed object
Your Enemy class and tracker class may need some way of communicating between themselves
Your tracker class may actually be a Pool type class, so you may want to do a search on object pooling
Big thank you for taking the time to compose such a detailed response, i really appreciate it. I have completed a book by Terry Norton on the basics on programming and it helped a great deal in understanding and reading code. The issue i’m facing at the moment is some of the logic behind the construction.
Your reply above is exactly what i needed to help better understand the logic behind some of the steps, especially from a seasoned coder’s perspective.
I will post a reply whenever i have completed the scripts which you have suggested above.