Search code examples
c#functiontagstoken

trying to delete a gameobject after a different gameobject collision - c# unity difficulty in regard to a function for destroying all items with a tag


i am trying to make a game and this includes a fence that the player can only go through once they located the key. the fence is a trigger so can be walk through but there is an object behind acting as the barrier. i am trying so that if the player has the key and collides with the fence, the barrier will disappear on all fences.

i am getting the errors "Assets\playerController.cs(18,17): error CS1002: ; expected" and "Assets\playerController.cs(18,30): error CS1519: Invalid token ';' in class, record, struct, or interface member declaration" ,but if i add the semi colon it still gives me the "Assets\playerController.cs(16,28): error CS1519: Invalid token ';' in class, record, struct, or interface member declaration" error.

`

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

namespace scripting // <-- This is a namespace
{
    public class playerController : MonoBehaviour
    {
        public float moveSpeed = 1f;
        public float collisionOffset = 0.05f;
        public ContactFilter2D movementFilter;
        public bool gotKey = false;

        public List<string> items;
        public GameObject[];

        Vector2 movementInput;
        Rigidbody2D rb;
        List<RaycastHit2D> castCollisions = new List<RaycastHit2D> ();

        // Start is called before the first frame update
        void Start()
        {
            items = new List<string>();

        
            rb = GetComponent<Rigidbody2D>();
        }
   

        private void FixedUpdate()
        {
            if(movementInput != Vector2.zero)
            {
                int count = rb.Cast(
                    movementInput,
                    movementFilter,
                    castCollisions,
                    moveSpeed * Time.fixedDeltaTime + collisionOffset);
                if(count == 0)
                {
                    rb.MovePosition(rb.position + movementInput * moveSpeed * Time.fixedDeltaTime);
                }
           
            }
        }
        void OnMove(InputValue movementValue)
        {
            movementInput = movementValue.Get<Vector2>();
        }
        private void OnTriggerEnter2D(Collider2D collision)
        {
            if (collision.CompareTag("collectable"))
            {

                string itemType = collision.gameObject.GetComponent<collectable_script>().itemType;
                print("we have collected a " + itemType);

                items.Add(itemType);
                print("Inventory length:" + items.Count);


                Destroy(collision.gameObject);
            }
            else if (collision.CompareTag("key"))
            {
                print("colliding!");
                gotKey = true;
                if (gotKey == true)
                {
                    print("yay!");
                }
                Destroy(collision.gameObject);
            }
            else if (collision.CompareTag("fence"))
            {
                print("fence!");
                if (gotKey == true)
                {
                    
                    GameObject[] fencestop = GameObjects.FindGameObjectsWithTag("fence barrier");

                    foreach (GameObject item in fencestop)
                    {
                        Destroy(item);
                    }
                    
                }
                
            }
            
        }
    }
}

`


Solution

  • The line after

    public List<string> items;
    

    You wrote

    public GameObject[];
    

    All fields must have their own names, like 'items', this line you declared a field without a name.