Question about findObject and error

Updated on July 11, 2016 in Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
8 on July 9, 2016


using UnityEngine;

using System.Collections;
public class vc : MonoBehaviour {
public GameObject player;

public zs[] _enemy; // enemy script
// Use this for initialization

void Start () {
}
// Update is called once per frame

void Update () {
}
void OnTriggerEnter(Collider col)

{

foreach(var e in _enemy)

{

if (col.gameObject.CompareTag("area") && e.enemy.activeSelf == false)

{

e.delete_enemy();

Debug.Log("delete");

}
if (col.gameObject.CompareTag("area") && e.enemy.activeSelf == true)

{

e.Enemey_respwand();

Debug.Log("full");

}
}
}

}


I’m trying to make something new in my code
It give me this error:
NullReferenceException: Object reference not set to an instance of an object
vc.OnTriggerEnter (UnityEngine.Collider col) (at Assets/vc.cs:23)

Second is this:
Can instead use foreach and remove “public zs[] _enemy;” use:
if(col.gameObject.CompareTag(“area”) && GameObject.FindGameObjectsWithTag(“enem”) == false)
{
e.delete_enemy();
Debug.Log(“delete”);
}

  • Liked by
Reply
5 on July 9, 2016

second: yes, you can do that. 

For the first question:
im guessing the error is the if statement in the OnTriggerEnter. Which one in the if statement is the nullreference because theres a &&. Which one? the left or right of the if statement?

on July 10, 2016
second: yes, you can do that. 
From Moddwyn16MC

Give me example please.
 

For the first question:
im guessing the error is the if statement in the OnTriggerEnter. Which one in the if statement is the nullreference because theres a &&. Which one? the left or right of the if statement?
From Moddwyn16MC

I don’t know its not clear for me. to make it easy here is my project:
http://www.filedropper.com/fad

Helpful
on July 10, 2016

the script is kinda confusing. What exactly are you trying to accomplish? What is your game suppose to do?

on July 10, 2016

Thank you for you reply.
I want from this project to understand some points of making enemy in games. Most tutorials in YT not give you good explanation about the methods that game developer use in their games. 
If you touch/killed the red box “enemy” and then touch the “area” object you will see ERORR pop-up. 
——
Second thing, As what i said before, what if i don’t want use array and use tags. for example:

var objects = GameObject.FindGameObjectsWithTag("enemy");
 var objectCount = objects.Length;
 foreach (var obj in objects) {
//here i found myself with another problem. which is can't load function from enemy script like, "delete_enemy();" or "Enemey_respwand();"
}

Helpful
on July 10, 2016

i was asking whats your game suppose to do meaning, what do you have to do? Kill the enemy then go to area? kill the enemy to be able to go to the area? whats the goal?

on July 11, 2016

OK. “area” is a place for Quick Save point. I know how to code “Save and Load”. My problem is with enemy. Before my problem is when duplicate my enemy it not work well but i solve it by using array. Now, If the player die or load last save, should the enemy come back or not ? what if there are two enemies the player killed one and the other still life but his healthy bar is 10/100. Here is my point. Think what will happen if the player die before or after reaching the save area ?

What I’m trying to do is:
1/ If the player die before reaching the save area BOTH enemies should respwand (Both living with full healthy).
2/ If the player die after reach the save area ONLY the dead enemy will not respwand but the other one get full healthy. (One dead, the other get full healthy).

The first code i post is work even with that strange error. The Second question is just wondering if there another method faster than array so no need to assign all enemies  that i duplicated [50 enemies = 50 elements].
This code may help me but I don’t know how to get it run as the first one ?
 —
var objects = GameObject.FindGameObjectsWithTag(“enemy”); var objectCount = objects.Length; foreach (var obj in objects) {//here i found myself with another problem. which is can’t load function from enemy script like, “delete_enemy();” or “Enemey_respwand();”}

I hope now all things is clear.

Show more replies
  • Liked by
Reply
Cancel