- Home /
Assigning tag on runtime break loop in C# unity3D 4.3.
Hi, I am working on 2D game in unity 4.3. I have a single red and blue ball and I want to create their clones on runtime and adding into different array with different tag but what happen when I try to assign tag then its break loop and just run code on above of assigning tag code. Below is my code to create 5 5 red and blue balls.
int i =0;
int tag=1;
float differenceX = 0.25f;
float differenceY = 0.25f;
while(i<5){
Vector3 position = new Vector3(blueBall.transform.position.x+differenceX,blueBall.transform.position.y,blueBall.transform.position.z);
GameObject redBall1 = Instantiate(redBall,position,Quaternion.identity) as GameObject;
tag = tag+i;
redBall1.gameObject.tag = "RedBall"+tag;
Vector3 positionBlue = new Vector3(blueBall.transform.position.x+differenceX,blueBall.transform.position.y-differenceY,blueBall.transform.position.z);
GameObject blueBall1 = Instantiate(blueBall,positionBlue,Quaternion.identity) as GameObject;
blueBall1.gameObject.tag = "BlueBall"+tag;
redBallsArray[i] = redBall1;
blueBallsArray[i] = blueBall1;
differenceX =differenceX+0.25f;
i++;
}
Code break from "redBall1.gameObject.tag = "RedBall"+tag;" and not execute code after that and when I block this line then it works fine and create all ball otherwise its just show only only one ball. I also tried with "blueBall1.transform.tag" and "blueBall1.tag". Kindly tell me any other approach to assigning tag or other technique to store each object with different identity in array. Thanks in advance.
Do you have required tags defined in tags manager? Also, please check for any errors in the console.
@ArkaneX, yes I added tags over there and also assign to them. I am getting Exception "UnityException: Index out of bounds. ThrowBall.FixedUpdate () (at Assets/Scripts/ThrowBall.cs:49)" and it continuously showing me on console.
I don't know what is the code in line 49 of ThrowBall class. I need to see it (if possible with some context) to be able to help.
@ArkaneX Thanks and Here are the code from line 48 to 57.
void FixedUpdate() { if(Input.GetTouch(0).phase == TouchPhase.Began) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position); if(Physics.Raycast(ray,out hit) && (hit.collider.name == "Sphere")) { Debug.Log("Object hit with Dragable child object..."); dragging = true; } }
Where I am getting touch from user. and this is line "if(Input.GetTouch(0).phase == TouchPhase.Began)" on 49. $$anonymous$$indly guide me on this.
Please change the line to:
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
This prevents accessing dynamic touches array when no touch is detected.
Answer by GameVortex · Jan 13, 2014 at 09:08 AM
Tags needs to exist in the **Tag List settings** in the editor prior to entering playmode in order to be assigned. In your case I would think it is better to use the name property.
redBall1.gameObject.name = "RedBall"+tag;
@GameVorex Thanks, you saved my day from tiny issue. Thanks again. Can you please tell me why I am getting this error ""UnityException: Index out of bounds. ThrowBall.FixedUpdate () (at Assets/Scripts/ThrowBall.cs:49)" and it continuously showing me on console."