• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by devsanunity · Apr 12, 2020 at 06:50 AM · scripting problemunityeditornot working

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);


 }

}alt text

,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);


 }

}

error2.png (251.6 kB)
error.png (238.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

229 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Foreach loops only runs once, on one object in an array. 1 Answer

Unity Script not working in build, but does work in play mode 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges