parsing error

I have some code written in Csharp I am getting a parsing error on for some unknown reason.
The error is on the public class line - Assets/scripts/switch.cs(4,19): error CS8025: Parsing error

using UnityEngine;
using System.Collections;

public class switch : MonoBehaviour {
public gameObject ammo1;
public gameObject ammo2;

void Start ()
{
    ammo1 = GetComponent<gameObject>();
	ammo2 = GetComponent<gameObject>();
}


void Update ()
{
    if(Input.GetKeyDown("1"))
    {
        ammo1.enabled = !ammo1.enabled;
    }
	
	else if (Input.GetKeyDown("2"))
	{
		ammo2.enabled = !ammo2.enabled;
	}
}

}

I suppose ammo1 and ammo2 are GameObjects, and if you want to (des)activate a gameObject you have to use gameObject.SetActive(bool)

so your code should looks like something like this

if(ammo1.activeSelf) //gameObject.activeSelf = "the  local active state of the GameObject"
      ammo1.SetActive(false);
else
      ammo1.SetActive(true);

You can simplify the code by

ammo1.SetActive(ammo1.activeSelf ? false : true);

or even

ammo1.SetActive(!ammo1.activeSelf);

Now be carefull because of few things

  • It’s ammo1 = GetComponent<GameObject>(); with a “G” instead of “g”. It wont work with ‘gameObject’
  • But actually, don’t Get the GameObject component, use something like GetComponent<Transform>() instead (so don’t forget to declare your variables as Transform too, and to do ammo1.gameObject.SetActive(...) ) OR declare ammo1 and ammo2 as ‘public’ and drop directly the GameObject into the Inspector
  • Don’t put this script on the object you want to desactivate, because it won’t be able to reactivate himself
  • ammo1 and ammo2 are exactly the same in your code because you initialize them in the start with the same thing.

So your code fully corrected would be something like

using UnityEngine;
using System.Collections;

public class Switch : MonoBehaviour {

	public GameObject ammo1;
	public GameObject ammo2;

	/*
	 *  Put the script on a manager, don't put it
	 *  on a GameObject who will be desactivated
	 */

	void Start ()
	{
		//Drop them directly in the inspector

		//ammo1 = GetComponent<gameObject>();
		//ammo2 = GetComponent<gameObject>();
	}
	
	
	void Update ()
	{
		if(Input.GetKeyDown("1"))
		{
			ammo1.SetActive(!ammo1.activeSelf);
		}
		
		else if (Input.GetKeyDown("2"))
		{
			ammo2.SetActive(!ammo2.activeSelf);
		}
	}

}

EDIT : i just noticed you have declared ammo1 and ammo2 as “gameObject”. It won’t work, you have to declare as “GameObject”.