Quantcast
Channel: Questions in topic: "findgameobjectswithtag"
Viewing all 230 articles
Browse latest View live

How to find one object in specific layer?

$
0
0
For my game work, I need to separate the objects with the same tag and check of both are the same. How to find one object in specific layer?

How to find a certain Gameobject using a variable, in a common script from an array of gameobjects?

$
0
0
Hi all, I have a need to locate a certain gameobject in the scene at runtime. I have a whole bunch of doors that are all from instantiated prefabs, but when they are created they are given a unique door number. Basically, I need to find that number in script and assign it to a target. Here are excerpts of my scripts: GameObject[] doors; doors = GameObject.FindGameObjectsWithTag("Door"); Ok, so now I have an array of Doors, that all have the script "Door Controller" on them. There is a unique int called "Door Number". So, my question is how can I find the gameobject in this array, that has a certain door number? Any help would be greatly appreciated :)

GameObject.FindGameObjectsWithTag not working after LoadLevelAdditiveAsync

$
0
0
Hi, Consider the following: void Start () { StartCoroutine (this.LoadLevelInBackground()); } private IEnumerator LoadLevelInBackground() { yield return Application.LoadLevelAdditiveAsync ("SomeLevel"); this.LoadReady(); } private void LoadReady() { Debug.Log("Destroying current menu canvas..."); Destroy(GameObject.FindWithTag("MenuCanvas")); Debug.Log("Activating level contents..."); foreach (GameObject gameObject in GameObject.FindGameObjectsWithTag("LevelContents")) { Debug.Log("Activating " + gameObject); gameObject.SetActive(true); } } The objects with the tag "LevelContents" come from the SomeLevel scene that is additively loaded. Unfortunately, the GameObject.FindGameObjectsWithTag always returns an empty array even though I can see the objects in the Inspector already loaded into the current scene. Is there a way to make this work? Thanks in advance.

Help with GameObject.FindGameObjectsWithTag

$
0
0
So I am trying to find all the enemies in the game when the missile is shot. The problem I'm having is that GameObject.FindGameObjectsWithTag is returning an array of length zero instead of giving me an array of all the enemies. I've checked to make sure that I do in fact have enemies in the game with the "Enemy" tag on them. Heres my code for it. Im hoping it just something small or I forgot to click something but any help would be greatly appreciated! public float bulletSpeed; public float rotSpeed; public GameObject[] potentialTargets; public float maxRange; public float maxAngle; GameObject lockedTarget; void Start(){ if (potentialTargets == null) { potentialTargets = GameObject.FindGameObjectsWithTag("Enemy"); } Debug.Log(potentialTargets); if(potentialTargets.Length == 0) { Debug.Log("Length is zero"); } foreach (GameObject target in potentialTargets) { float dist = Vector2.Distance(transform.position, target.transform.position); float angle = Vector2.Angle(transform.position, target.transform.position); Debug.Log(dist); Debug.Log(angle); if(dist < maxRange && angle < maxAngle) { lockedTarget = target; } } }`

transform all GameObjects with the tags("..") C#

$
0
0
I am struggling with my code and I can't find why. I want to move all object (with the tag RED,BLUE or GREEN) in the scene to the right. This action will happen when the left mousebutton is pressed. the Objects in the scenes do have the tags RED,BLUE or GREEN, one of these attached to them. I have tried to use `GameObject.FindGameObjectWithTag("Desired Tag")` But this would only Move 1 object with the tags. Also if `GameObject.FindGameObjectWithTag("RED").transform.Translate(Direction*movespeed);` is called. It wont call the other 2 line of code with BLUE and GREEN (Line 21 and 22). I know the code beneath is wrong, because I should have written FindGameObjectsWithTag("RED") to FindGameObjectsWithTag("RED")[5]. But what I have understand is that the number at the end will only target the 5th object with the tag: "RED". Another thing I tried is, instead of Void Update... -> `Void OnMouseDown(){ FindGameObjectWithTag("RED").transform.Translate(Direction*movespeed); FindGameObjectWithTag("GREEN")tansform.Translate(Direction*movespeed); etc.. }` But I get the same results. - The Objects with the tags will be Instantiated by another script. but I don't that is relevant for this problem. using UnityEngine; using System.Collections; public class OGMovement : MonoBehaviour { public int movespeed; private Vector3 Direction; // Use this for initialization void Start () { movespeed = 1; Direction = Vector3.right; } // Update is called once per frame void Update () { if (Input.GetMouseButtonDown (0)) { GameObject.FindGameObjectsWithTag("RED").transform.Translate (Direction * movespeed); GameObject.FindGameObjectsWithTag("BLUE").transform.Translate (Direction * movespeed); GameObject.FindGameObjectsWithTag("GREEN").transform.Translate (Direction * movespeed); } } } - Sorry for the long text, but I wanted to explain everything. Thx -Hexer

AddComponent To GameObjects On Array

$
0
0
Hello, is it possible for me to add the component "TimeGate" to every object with the tag "PhysicsObject"? Whenever I try, I get the error "(15,53): BCE0138: 'quack' is not a generic definition" Here is the code: var collisionObject : GameObject; var physicsObjects : GameObject[]; function OnTriggerExit(collision : Collider) { collisionObject = collision.gameObject; if(collisionObject.tag == "Player") { physicsObjects = GameObject.FindGameObjectsWithTag("PhysicsObject"); if(!collisionObject.GetComponent("TimeGate")) { collisionObject.AddComponent.(); physicsObjects.AddComponent.(); } } }

How Expensive is Find function?

$
0
0
having > var noiseControl :NoiseControl; At the moment i feed this variable through the inspector. Yet, for mobile game, how expensive will it be to just find it on Awake? Is it alright to initiate around 20 'Find's at startup assuming i have around 150-200 objects in scene? or perhaps have each object it's unique tag and FindObjectsWithTag? is it so heavy i should not consider it at all?

Best method to manage multiple tags

$
0
0
I would like to know if there's an eloquent way to use multiple tags in Unity. A similar question was asked [here][1], but that was several years ago. I feel that the accepted answer (add child GameObjects and tag them), is inelegant. There's an answer that suggests you could use compound tags (such as "enemy,champion,killable"), and then check if the term you are searching for is included in the tag, but I don't see how you can get the GameObject in the first place to check the tag. After all, FindGameObjectsWithTag() requires an exact match. I've also heard from some people that you should avoid using Find functions altogether. So, my question to the experienced developers is this: How do you use multiple tags in Unity? Alternatively, if you prefer to avoid tags, how do you access GameObjects from other GameObjects? [1]: http://answers.unity3d.com/questions/21664/is-it-possible-to-have-multiple-tags-or-the-like-o.html

Destroy all gameObjects with tag in one line

$
0
0
I love super efficient code, and wondered if there's a way to destroy all gameObjects with the same tag in just one line without looping through like this? gameObjects = GameObject.FindGameObjectsWithTag ("yourTag"); for(var i = 0 ; i < gameObjects.length ; i ++) Destroy(gameObjects[i]); If it's not possible, then I'll do the for loop, but I thought there might be a super concise way :-)

FindGameObjectsWithTag finds an object that doesn't exist

$
0
0
Hi, I use the GameObject[] tag_object=FindGameObjectsWithTag("tag"); to determine if an object exists. If no such object exists, tag_object.Length should be 0. But sometimes tag_object.Length returns 1 although no such object exists. I can't even find it in the Hierarchy window. Is this a bug of unity? I am using version 5.0.1f1. Thanks!

How to cut off an array of gameobjects?

$
0
0
Hello, I created a script which changes behavior of a camera in 2D. The behavior is changed by other script attached to triggers in the scene simply by setting bool. Then in my camera script I want to find all the triggers with tag and deside if the bool in other script is true then do something. Everything works fine, but only with the first find component. Always changes behavior the first trigger but not the others. I thing the problem is in my script bellow when working with an array. Any help please? private ChangeCameraBehavior changeCameraBehavior; void Awake () { GameObject[] changeCamBehaviorCubes = GameObject.FindGameObjectsWithTag ("ChangeBehavior"); for (int i = 0; i < changeCamBehaviorCubes.Length; i++) { changeCameraBehavior = changeCamBehaviorCubes[i].GetComponent(); } } void Update () { if(changeCameraBehavior.breakPattern == true) { //do something }

Better solution for GameObject.FindGameObjectsWithTag

$
0
0
Hi, In my game all the time people walk around, get destroyed and get created. Every 5-10 seconds I need to take a random gameobject and make him do something. But what is the best way to choose a random gameobject. I can use GameObject.FindGameObjectsWithTag but I hope their is a more performance friendly solution. What I have a.t.m. is the following: GameObject[] ArrayEnterPeople = GameObject.FindGameObjectsWithTag("People"); int RandomNumb = Random.Range(0,ArrayEnterPeople.Length); GameObject EnterPeople = ArrayEnterPeople[RandomNumb]; Could someone please help me with my problem?

strange behaviour when creating and deleting objects with tags

$
0
0
hi. i am observing a very strange behavior in my project. not sure, if i am just missing something or it is a bug. I am creating GameObjects at runtime and give them tags. `GameObject currGarage = new GameObject (); currGarage.tag = "Garage";` later, i create a list and add objects with this tag to the list: GameObject[] garageArray = GameObject.FindGameObjectsWithTag ("Garage"); foreach(GameObject g in garageArray) { if(g != null) { allGarages.Add(g); } else { Debug.Log("object null"); } } then, at some point, i delete all objects in this list: foreach (GameObject g in appLogic.garageManager.allGarages) { g.SetActive(true); Destroy (g); } after that i create again some new GameObjects with that tag and write them into a new array: GameObject[] garageArray = GameObject.FindGameObjectsWithTag ("Garage"); now i have an array of the size of all Gameobjects with that tag ever created. even if i delete objects, unity keeps referencing them. Am i doing something wrong? Or is it normal behaviour? I just want the "fresh" objects, without references to the destroyed ones... someone knows more about this issue?

checking if gameobject exists

$
0
0
Hi. I use this code to deactivate gameobjects. GameObject[] rigid; void Start() rigid = GameObject.FindGameObjectsWithTag("klocek"); foreach(GameObject rigids in rigid) { if(rigids.transform.position.x>30) rigids.SetActive(false); } } And then I need to activate them somewhen void Update(){ foreach(GameObject rigids in rigid) { if(rigids.transform.position.x

using GameObject.Find and Cpu usage

$
0
0
So for the most part ive been using GameObject.Find for my audio masters, long story short im finding an object and referencing a specific script attached to that object, however ive heard that GameObject.Find consumes a lot of cpu and its much better to find with tags which i have been doing for the most part Anyways, for this problem i need to use find for reasons that would take to long to explain but ive found a workaround and i think it might be less consuming on the cps, let me know private GameObject spikesound; void Start () { GameObject AudioObj = GameObject.FindWithTag("AudioMaster"); spikesound = AudioObj.transform.Find ("TimedSpikeAudio").gameObject; will this be less consuming of my cpu? thought id querry it with you before i delved into changing everything

i have a 2d infinite runner game and I want to get the position of the nearest platform so I could respawn the player there.

$
0
0
hello I have a 2d infinite runner game, and I want it to respawn to the nearest platform. I actually already created a respawn method but It is very wrong, I just add 10 to the y and x position of the player PlayerPrefs.SetFloat("XCheckPoint",(Player.position.x+10)); PlayerPrefs.SetFloat("YCheckPoint",(Player.position.y+10)); * the tag of the platform is invisiblePlatform * the Player is Instantiated so there might be a problem if you declare something that you must drag and drop. * Basically all I want is just to get the position of the nearest platforms

how does the unity docu work : GameObject.FindGameObjectsWithTag

$
0
0
i found this from unity docs, the second example. http://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html // Find the name of the closest enemy GameObject FindClosestEnemy() { GameObject[] gos; gos = GameObject.FindGameObjectsWithTag("Enemy"); GameObject closest = null; float distance = Mathf.Infinity; Vector3 position = transform.position; foreach (GameObject go in gos) { Vector3 diff = go.transform.position - position; float curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = go; distance = curDistance; } } return closest; } } i wanna use this script because I have a 2d infinite runner game that when the player falls and dies I want him to respawn towards the closest platform, I actually have already created a spawn script but what I did is just add 10 to the x and y position, and another problem is I don't get this script/I dont understand this. all I want is the coordinates of the closest Platform.

Finding closest object to player

$
0
0
I'm working on a cover system for a stealth game. In general it's working fairly well except for one thing. When the player goes into cover, I have a script (below) that finds the closest cover object (walls, pedestals, etc.). The problem is, with long walls, the transform of the cover object is sometimes farther away than other cover pieces so the detection determines that other cover pieces are closer which is, less than helpful. If anyone has any thoughts on how to efficiently find the closest object based on it's colliders or vertices or bounds or something else I'm not thinking of, that'd be awesome. Thanks! void FindNear() { GameObject[] gos = GameObject.FindGameObjectsWithTag("Cover"); Vector3 position = transform.position; float coverDist = coverDistance; foreach (GameObject go in gos) { Vector3 diff = go.transform.position /*Find Closest point on go*/ - position; float curDistance = diff.sqrMagnitude; if (curDistance < coverDist) { closest = go; coverDist = curDistance; } } }

finding game objectS with tag as recttransform

$
0
0
I have a problem about finding objectS. I need to find some UI buttons and make them last sibling. The problem is that SetAsLastSibling () function is property of RectTransform. Is there any way to find them as recttransform with tag or get recttransform component? public class SilahYerlestirme : MonoBehaviour { public GameObject[] silahKoymaTuslari; public void SilahPanelAcma (....) { silahKoymaTuslari = GameObject.FindGameObjectsWithTag ("SilahKoymaButtonTag"); foreach (GameObject item in silahKoymaTuslari) { silahKoymaTuslari.SetAsLastSibling (); } } } I am sorry about my English and thanks for your help.

Declaring a variable in Start funciton

$
0
0
I'm following the unity documentation and I found exactly what I was looking for. The sample script in the documentation is // Search for game objects with a tag that is not used function Start () { var gos : GameObject[]; gos = GameObject.FindGameObjectsWithTag("fred"); if (gos.length == 0) { Debug.Log("No game objects are tagged with fred"); } } I did pretty much the same thing. using UnityEngine; using System.Collections; public class DoorOpen : MonoBehaviour { void Start () { var enemies : GameObject[]; enemies = GameObject.FindGameObjectsWithTag("Enemy01"); } void Update () { if (enemies == null || enemies.Length == 0) { Destroy (this.gameObject); Debug.Log ("Enemies array has no enemies in it."); } if (enemies.Length > 0) { Debug.Log ("Enemies array has enemies in it."); } } } However, this doesn't work. I get this error. `Assets/DoorOpen.cs(7,29): error CS1525: Unexpected symbol `:', expecting `)', `,', `;', `[', or `='`
Viewing all 230 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>