Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 aleem.razzaq09 · Jan 13, 2014 at 08:49 AM · arraytagunity4.3

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.

Comment
Add comment · Show 6
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
avatar image ArkaneX · Jan 13, 2014 at 09:05 AM 0
Share

Do you have required tags defined in tags manager? Also, please check for any errors in the console.

avatar image aleem.razzaq09 · Jan 13, 2014 at 10:56 AM 0
Share

@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.

avatar image ArkaneX · Jan 13, 2014 at 12:01 PM 0
Share

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.

avatar image aleem.razzaq09 · Jan 13, 2014 at 12:32 PM 0
Share

@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.

avatar image ArkaneX · Jan 13, 2014 at 12:41 PM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;


Comment
Add comment · Show 2 · Share
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
avatar image aleem.razzaq09 · Jan 13, 2014 at 11:12 AM 0
Share

@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."

avatar image GameVortex · Jan 13, 2014 at 11:14 AM 0
Share

No problem, glad I could help.

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.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Array loads in wrong order 3 Answers

Store Vector3 from Array of GameObjects to Vector3Array 1 Answer

How to set game scene according to iPad and iPhone for 2D game in Unity4.3 1 Answer

if collision.gameObject.tag == any of the elements in the array... 0 Answers

Initialize Vector3 2 Dimensional Array 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