- Home /
GameObject.FindGameObjectsWithTag not working
Hi
Im new to Unity coding and Im building my first application. I am not able to detect game objects with the tag Player though i have assigned the same to my game objects in the Inspector. This is an AR tutorial from YouTube and the entire script can be found on : https://github.com/rubiin/Ar-SpaceShip
The collision script in which the problem is reflecting is mentioned below. The console displays a warning - the variable enemy , enemy 1, etc is assigned but its value is never used
Basically the first four enemy objects are appearing but they are not respawning after they have been destroyed. I am building out on Android
using UnityEngine; using System.Collections;
public class collisionScript : MonoBehaviour {
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
//for this to work both need colliders, one must have rigid body (spaceship) the other must have is trigger checked.
void OnTriggerEnter(Collider col)
{
GameObject explosion = Instantiate(Resources.Load("TinyExplosion", typeof(GameObject))) as GameObject;
explosion.transform.position = transform.position;
Destroy(col.gameObject);
Destroy(explosion, 2);
if (GameObject.FindGameObjectsWithTag("Player").Length == 0)
{
GameObject enemy = Instantiate(Resources.Load("enemy", typeof(GameObject))) as GameObject;
GameObject enemy1 = Instantiate(Resources.Load("enemy1", typeof(GameObject))) as GameObject;
GameObject enemy2 = Instantiate(Resources.Load("enemy2", typeof(GameObject))) as GameObject;
GameObject enemy3 = Instantiate(Resources.Load("enemy3", typeof(GameObject))) as GameObject;
}
Destroy(gameObject);
}
}
,Hi
Im new to Unity coding and the GameObject.FindGameObjectsWithTag("Player") line is not working in the script below. In the inspector I have assigned the objects with Player tag. This is an AR tutorial from Youtube and the entire script can be found on:
https://github.com/rubiin/Ar-SpaceShip
using UnityEngine; using System.Collections; public class collisionScript : MonoBehaviour {
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
//for this to work both need colliders, one must have rigid body (spaceship) the other must have is trigger checked.
void OnTriggerEnter(Collider col)
{
GameObject explosion = Instantiate(Resources.Load("TinyExplosion", typeof(GameObject))) as GameObject;
explosion.transform.position = transform.position;
Destroy(col.gameObject);
Destroy(explosion, 2);
if (GameObject.FindGameObjectsWithTag("Player").Length == 0)
{
GameObject enemy = Instantiate(Resources.Load("enemy", typeof(GameObject))) as GameObject;
GameObject enemy1 = Instantiate(Resources.Load("enemy1", typeof(GameObject))) as GameObject;
GameObject enemy2 = Instantiate(Resources.Load("enemy2", typeof(GameObject))) as GameObject;
GameObject enemy3 = Instantiate(Resources.Load("enemy3", typeof(GameObject))) as GameObject;
}
Destroy(gameObject);
}
}
Your answer
Follow this Question
Related Questions
IAP doesn't work in google play store (on device) but works fine in the editor. 0 Answers
How to make these scripts work together? 0 Answers
Unity - Cut a sprite in 2 halfs using the form of another sprite 0 Answers
If two PlayerPrefs are on? 2 Answers
How do I make an unusable and hidden button until certain conditions are met ? 1 Answer