Reputation: 175
When using a script to hide and unhide specific sprites on the click of a button, I get these two errors.
Assets\Scripts\ButtonLeft.cs(26,5): error CS1061: 'int' does not contain a definition for 'SetActive' and no accessible extension method 'SetActive' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)
Assets\Scripts\ButtonLeft.cs(22,8): error CS1061: 'int' does not contain a definition for 'activeSelf' and no accessible extension method 'activeSelf' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)
I was assured by previous posts, some only dating back a few weeks, that activeSelf
was the correct way to check whether a sprite was already hidden, and I have used SetActive
before in this project with no issues. Here's the script I'm trying to run:
using System.Collections.Generic;
using UnityEngine;
public class ButtonLeft : MonoBehaviour
{
public void Click ()
{
if (23.activeSelf)
{
23.SetActive (false);
3.SetActive (true);
1.SetActive (false);
}
if (3.activeSelf)
{
23.SetActive (false);
3.SetActive (false);
1.SetActive (true);
}
if (1.activeSelf)
{
23.SetActive (true);
3.SetActive (false);
1.SetActive (false);
}
}
// Update is called once per frame
void Update()
{
}
}
Is the syntax wrong in any way? Is activeSelf not the correct way to check whether an object is active or not?
Using Unity 2019.4.5f1 Personal
Upvotes: 1
Views: 11747
Reputation: 1836
The reason you're getting those error messages is due to running them on integers instead of sprites. You need to use an instance of a GameObject
.
You can acquire a reference to something of that sort in several ways. One would be by making a field that's exposed to the inspector and providing the reference through there.
using UnityEngine;
public class ButtonLeft : MonoBehaviour
{
[SerializeField]
private SpriteRenderer sprite1 = null;
[SerializeField]
private SpriteRenderer sprite3 = null;
[SerializeField]
private SpriteRenderer sprite23 = null;
public void Click()
{
if (sprite23.gameObject.activeSelf)
{
sprite23.gameObject.SetActive(false);
sprite3.gameObject.SetActive(true);
sprite1.gameObject.SetActive(false);
}
if (sprite3.gameObject.activeSelf)
{
sprite23.gameObject.SetActive(false);
sprite3.gameObject.SetActive(false);
sprite1.gameObject.SetActive(true);
}
if (sprite1.gameObject.activeSelf)
{
sprite23.gameObject.SetActive(true);
sprite3.gameObject.SetActive(false);
sprite1.gameObject.SetActive(false);
}
}
}
using UnityEngine;
public class ButtonLeft : MonoBehaviour
{
[SerializeField]
private GameObject sprite1 = null;
[SerializeField]
private GameObject sprite3 = null;
[SerializeField]
private GameObject sprite23 = null;
public void Click()
{
if (sprite23.activeSelf)
{
sprite23.SetActive(false);
sprite3.SetActive(true);
sprite1.SetActive(false);
}
if (sprite3.activeSelf)
{
sprite23.SetActive(false);
sprite3.SetActive(false);
sprite1.SetActive(true);
}
if (sprite1.activeSelf)
{
sprite23.SetActive(true);
sprite3.SetActive(false);
sprite1.SetActive(false);
}
}
}
Upvotes: 1