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
Your answer
