• 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 /
  • Help Room /
avatar image
0
Question by Daniele122898 · Jan 08, 2017 at 09:30 AM · gameobjectdeletefindwithtag

Will delete objects in Editor but not in Build?

So i have a problem. In my Menu Screen i have an arsenal tab. There is a table where the gun hovers above it and when i hit the left arrow or right arrow it switches trough the weapons showing their prefab in the screen.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ArsenalManager : MonoBehaviour {
 
     public GameObject[] guns;
     public Transform spawnPosition;
     public int gunIndex;
     Transform instantiatedGun;
     int[] levelNeeded = new int[5];
     int currentLevel;
 
     
 
     private void Start()
     {
         for(int i = 0; i< guns.Length; i++)
         {
             levelNeeded[i] = i * 5;
         }
     }
 
     public void OnCall()
     {
         RenderGun();
     } 
 
     void RenderGun()
     {
         //GameObject toDelete = GameObject.FindGameObjectWithTag("ArsenalWeapon");
         DestroyImmediate(GameObject.FindGameObjectWithTag("ArsenalWeapon"));
         Transform clone = Instantiate(guns[gunIndex].transform,spawnPosition.position, Quaternion.Euler((gunIndex==0)?180:0 ,-90,0));
     }
 
     public void LeftButton()
     {
         if (gunIndex > 0)
         {
             gunIndex--;
             RenderGun();
         }
         else
         {
             return;
         }
     }
 
     public void RightButton()
     {
         if (gunIndex < 4)
         {
             gunIndex++;
             RenderGun();
         }
         else
         {
             return;
         }
     }
 
     public int[] OnAcceptClick()
     {
         int[] trueAndLevel = new int[2];
         currentLevel = PlayerPrefs.GetInt("level", 0);
         if(currentLevel >= levelNeeded[gunIndex])
         {
             PlayerPrefs.SetInt("currentgun", gunIndex);
             trueAndLevel[0] = 1;
             trueAndLevel[1] = 0;
             return trueAndLevel;
         }
         trueAndLevel[0] = 0;
         trueAndLevel[1] = levelNeeded[gunIndex];
         return trueAndLevel;
     }
 
 }

Now i assigned the Tag to the weapons and in the editor when i run the game it works perfectly fine. when i hit left or right the old gun gets deleted and the new model is showed.

As soon as i built it it doesnt work anymore.

I even downloaded the EditorTag Finder script and modivied it to possibly find those tags but it tells me there are no editor tags. what is the problem?

 // xeophin.net/code // // (c) 2010 Kaspar Manz // code@xeophin.net // // All rights reserved. //
 using System;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 [AddComponentMenu("Helper Scripts/List Objects By Tag")]
 
 ///
 
 /// This is a simple little script that lists all objects with a /// certain tag, in order to find wrongly tagged objects. ///
 public class TagFinder : MonoBehaviour
 {
     /// <summary>
     /// A list of tags to look for.
     /// </summary>
     public string[] tagsToFind = { "EditorOnly" };
     string sceneName;
 
     /// <summary>
     /// At the start of the game, this script lists all objects
     /// with the defined tags.
     /// </summary>
     void Start()
     {
         findTags();
         sceneName = SceneManager.GetActiveScene().name;
 
     }
 
     void findTags()
     {
         foreach (string tag in tagsToFind)
         {
             print("Objects tagged with '" + tag + "':");
 
             try
             {
                 GameObject[] objects = GameObject.FindGameObjectsWithTag(tag);
                 if (objects.Length != 0)
                 {
                     foreach (GameObject item in objects)
                     {
                         print("  " + item.name + " on layer " + item.layer);
                     }
                 }
                 else
                 {
                     print("  There are no objects with the tag '" + tag + "'.");
 
                 }
             }
             catch (UnityException ex)
             {
                 print("  The tag '" + tag + "' has not been found. Exception Message: " + ex.Message);
             }
 
         }
     }
 
     private void Update()
     {
         if(SceneManager.GetActiveScene().name != sceneName)
         {
             sceneName = SceneManager.GetActiveScene().name;
             findTags();
         }
 
         if (Input.GetKeyDown(KeyCode.T))
         {
             findTags();
         }
     }
 }
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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

103 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

Related Questions

How to assign GameObject to a instantiated prefab via Script(C#)? 2 Answers

How can you change a Ridgidbody2D from Kinematic to Dynamic at runtime with GameObject.FindWithTag("myObject") ? 0 Answers

Cannot properly access player position 1 Answer

Transform does not contain a definition of rotate 1 Answer

How to keep objects in a scene even after reload? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges