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 Zanrok · Oct 15, 2012 at 08:44 PM · c#gameobjectinstantiatearrays

Remove GameObject From Array if it already exists?

This one has been stumping me for a while now. Essentially, I have a script setup that Creates a new GameObject Array for every letter typed into a string. The problem I having now is that every time I type something the GameObject gets added too and KEEPS the original GameObject. So for example... if I type "hi". The "h" will be created twice and the "i" once.

I only want to create the "h" once... so my idea was to delete the object if it exists before instantiating it again. I should note that if I delete the string entirely, the object still remains, so I'm hoping his solves that issue as well. Here is what I have so far in the update() function.

   byte[] ASCIIBytes = Encoding.ASCII.GetBytes(test);
 
             float po = 0;
             foreach (byte b in ASCIIBytes)
             {
 
                 Debug.Log(test);
                
                 // check if certain "letter" already exists, if it does.. 
                 // delete it before creating it again
 
                 Instantiate(testObj[b], new Vector3(po, 0, 0), Quaternion.Euler(270, -180, 0));
                 po += kerning;  
                 
                 testObj[b].transform.tag = "Word";
                
                
             }


Any help or advice would greatly appreciated. Thank you!

Comment
Add comment · Show 1
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 Zanrok · Oct 15, 2012 at 09:31 PM 0
Share

You are correct, I don't understand gameobject array too well. I think I know what are you are saying though. How would I create an array element in this example? Would this allow me to create "one" gameobject in the scene with several subobjects?

1 Reply

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

Answer by whydoidoit · Oct 15, 2012 at 09:08 PM

Yes that would happen! You will need to delete all of the letters you created before. When you instantiate them add them to a List<GameObject> and the next time - delete all the existing ones before creating the new ones.

Comment
Add comment · Show 3 · 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 Zanrok · Oct 15, 2012 at 09:33 PM 0
Share

Thanks for the response $$anonymous$$ike! I think I understand what are suggesting. I just have never used List<> before. How would I add the gameObjects to a List?

Would something like this work?

  List<GameObject> temp = new List<GameObject>(b);
avatar image whydoidoit · Oct 15, 2012 at 09:37 PM 0
Share

You would create the list as a private variable of the class:

   List<GameObject> currentLetters = new List<GameObject>();

You would add them as you instantiate them:

   currentLetters.Add(Instantiate(testObj[b]...));

But before you created the new ones you would do:

  foreach(var letter in currentLetters)
       Destroy(letter);
  currentLetters.Clear();

Now this all presumes that testObj[] is an array of GameObjects - it it's something else then you would need to get the gameObject from it. Eg. testObj[] is an array of Transform then:

   currentLetters.Add((Instantiate(...) as Transform).gameObject);

There info on using Lists here

avatar image Zanrok · Oct 15, 2012 at 11:28 PM 0
Share

Thanks so much $$anonymous$$ike!!

I was able to use this

 currentLetters.Add((Instantiate(testObj[b], new Vector3(po, 0, 0), Quaternion.Euler(270, -180, 0)) as GameObject)); 

and it did the trick perfectly! In combination with your suggested foreach code.

=)

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

11 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

Related Questions

How can I add the OnTriggerEnter function to all game objects that I instantiate? 1 Answer

Instantiated GameObject Not Rendering 1 Answer

Instantiate from array into array? 2 Answers

C# instantiate into list 2 Answers

Instantiate prefab at specific path 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