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 frankyboy450 · Jan 23, 2014 at 11:57 AM · c#arrayplayertagrender

Removing and Adding gameobjects to an array upon collision

Hello. I'm attempting to do something that might be a bit difficult. I'm making a 2D game where the player has 2 child objects attached to it. They both have a collider attached to them and are positioned to the left and to the right of the player, outside the camera.

The right one enables the MeshRendereron the floor(which is made of boxes) and the left one disables the MeshRenderer. I'm attempting to optimize this script so it also destroys these objects but I can't. because the player adds all of them to an array upon starting the game.

In the Start() I assign the PlayerScript to a variable called playerScript

private PlayerScript playerScript;

 void Start()
 {
     playerScript = GetComponent<PlayerScript>();
 }

But I'm unable to reach the array and modify it for some reason.

 if(other.CompareTag("Floor"))
 {
     if (renderObject == true)
     {
         //Add it to the array
     }
     else
     {
         //Remove it from the array
     }
 }

if I start typing playerscript.RedFloorArray nothing show up.

Thanks for reading

Comment
Add comment · Show 8
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 Josh707 · Jan 23, 2014 at 11:59 AM 0
Share

Is the array public in the PlayerScript? If not you will have to make it public or write public functions to set/get/modify the array

avatar image frankyboy450 · Jan 23, 2014 at 12:25 PM 0
Share

totally forgot to make it public.. How do I then modify the array? Will I have to reset it and set it again? I'm guessing this would cause lag if there's a lot of boxes.

avatar image njpatter · Jan 23, 2014 at 12:52 PM 0
Share

You could try using a List ins$$anonymous$$d. You can then add and remove using things like: mylist.Add(newItem); mylist.Remove(deadItem);

avatar image Jessy · Jan 23, 2014 at 01:41 PM 0
Share

List isn't the best option for a frequently-size-modified collection; HashSet is.

avatar image frankyboy450 · Jan 23, 2014 at 01:49 PM 0
Share

Oh ok, that seems easy enough. But I can't add gameobjects with a tag to it.

 RedFloorList.Add(FindGameObjectsWithTag("RedColoredFloor"));

Any other way?

Show more comments

1 Reply

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

Answer by giudark · Jan 23, 2014 at 01:59 PM

In PlayerScript you can simply create a function like this one you are trying to wrote outside (i assume that the RedFloorArray is an array of GameObject):

 public void AddFloorArray(){
  GameObject child = GameObject.FindGameObjectsWithTag("Floor") // or you can pass it as parameter
  RedFloorArray.Add(child);
 }
 
 public void RemoveFloorArray(){
  for(int i = 0; i<RedFloorArray.length; i++){
   obj = RedFloorArray[i];
   if(obj.CompareTag("Floor")){
     RedFloorArray.RemoveAt(i);
   }
   //maybe --> Destroy(obj);
  }
 }

  

  public void RemoveFloorArray(GameObject child){
      for(int i = 0; i<RedFloorArray.length; i++){
       obj = RedFloorArray[i];
       if(obj.name.Equals(child.name)){
         RedFloorArray.RemoveAt(i);
       }
       //maybe --> Destroy(obj);
      }
     }

So you can call this function outside without problem:

 OnCollisionEnter(Collision col){
  playerScript.RemoveFloorArray(col.gameObject);
 }
Comment
Add comment · Show 5 · 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 frankyboy450 · Jan 23, 2014 at 02:49 PM 0
Share

Is that a list? the RedFloorArray variable? Edit: All the floor blocks has the same name... so :/

avatar image giudark · Jan 23, 2014 at 04:46 PM 0
Share

in this example it is an Array (class), but it can be easly a list. You can modify this:

 if(obj.name.Equals(child.name))

in this:

 if(obj.tag.Equals(child.tag))

or implementing some other way to differentiate what you want to delete (for exaple if you know the index of the block):

 public void RemoveFloorArray(int index){
   GameObject obj = RedFloorArray[index];
   RedFloorArray.RemoveAt(index);
   //maybe --> Destroy(obj );
 }
avatar image frankyboy450 · Jan 23, 2014 at 06:38 PM 0
Share

I learnt that each item in the scene has a unique ID that I can find with .GetInstanceID()

but when I call the RemoveFloorArray(); I get this error:

 NullReferenceException: Object reference not set to an instance of an object
 RenderFloor.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/RenderFloor.cs:22)

This is the code that calls it:

 playerScript.RemoveFloorArray(other.gameObject);

And the Function modified a bit

 public void RemoveFloorArray(GameObject newObj)
     {
         Debug.Log("RemoveFloorArray Called");
         if(newObj.CompareTag("RedColoredFloor"))
         {
             foreach(GameObject redFloorObj in RedFloorList)
             {
                 if(redFloorObj.GetInstanceID().Equals(newObj.GetInstanceID()));
                 {
                     RedFloorList.Remove(redFloorObj);
                 }
             }
         }

the debug.log line doesn't get called, so I'm guessing something went wrong in calling it...

EDIT: Used this code ins$$anonymous$$d playerScript = transform.parent.gameObject.GetComponent();

which now calls the function. but the list is being accessed somewhere else in the playerscript and says the instance of the object was deleted...

avatar image frankyboy450 · Jan 23, 2014 at 08:41 PM 0
Share

NV$$anonymous$$. Changed it to disable the object ins$$anonymous$$d of destroying it. Works now. Thanks ;)

avatar image Astrydax · Mar 03, 2016 at 03:18 PM 0
Share

The Array class is only available in Javascript. http://docs.unity3d.com/ScriptReference/Array.html

Does this mean i cant use the RemoveAt method if im program$$anonymous$$g in C#?

RemoveAt() isnt showing in the autocomplete and is throwing a "does not contain a definition for 'RemoveAt'.

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

22 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 avatar image avatar image

Related Questions

[C#] how do I get the name of the object under the player? 1 Answer

Multiple Cars not working 1 Answer

Attacking more than one enemy with same tag, but unity only allows one enemy at a time? 2 Answers

Only instantiate GameObjects with a certain tag from an array 1 Answer

Question about FindObjectsOfType 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