dont know
dont know

Reputation: 11

Type `UnityEngine.GameObject' does not contain a definition for `setActive' (Unity)

I am making a game in Unity, where I need to disable and enable a gameObject, and I thought that itemBought.setActive(false); line (21,14) would work but it doesn't.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ItemManager : MonoBehaviour {

public HandInClicker handClicker; // sælg Assignments btn.
public AssClicker assClicker; // Assignments btn.
public AssBarControl assBarControl; // Loading Bar til Assignments.
public Button enableBuyBtn; // opgraderings btn.
public GameObject itemBought; // Når der er købt et item f.eks. "Pencil" så kommer det er billede frem så man ikke kan købe den igen.
public Text itemInfo; // Det er det text element som "itemName" skal stå i.
public string itemName; // Hvad opgraderingen er f.eks. en ny blyant.
public float apcUpgarde; // Hvor meget mere man får i "assPerClick" for opgraderingen.
private float baseCost; // hvormeget opgraderingerne koster når spillet starter.
public float cost; // Hvor meget opgraderingen koster.

void Start(){
    enableBuyBtn.interactable = false;
    itemBought.setActive(false);
    baseCost = cost;
}

void Update(){
    if (handClicker.handIn >= cost){
        enableBuyBtn.interactable = true;
    }
    itemInfo.text = itemName + "\nCost: " + "$" + cost.ToString("F0") + "\n" + "+" + apcUpgarde + " A/pc";
}

public void PurchasedUpgrade(){
    if(handClicker.handIn >= cost){
        handClicker.handIn -= cost;
        assClicker.assPerClick += apcUpgarde;
        enableBuyBtn.interactable = false;
        itemBought.setActive(true);
    }
}

}

What would work better/at all? Right now I'm getting:

error "(21,14): error CS1061: Type UnityEngine.GameObject' does not contain a definition for setActive and no extension method setActive of type UnityEngine.GameObject' could be found. Are you missing an assembly reference?"`

Upvotes: 1

Views: 4441

Answers (1)

SwiftArchitect
SwiftArchitect

Reputation: 48514

  1. Find the reference to the GameObject
  2. Invoke SetActive()

As an example, for a UI Button:

using UnityEngine.UI;
...
aButton.gameObject.SetActive (false);

As noted by @volundi, C# is case sensitive.

Upvotes: 1

Related Questions