Unity - SetActive not defined - c#

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

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);
}
}
}
How things may end up looking in the editor:
If you want to be able to drag in any game object as a reference
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);
}
}
}

Related

Unity 3d creating character movement - Error CS1061

I'm currently following a tutorial about game dev on Unity (by Natty Creations) and I'm getting an error which I cannot solve (sorry I'm very new to this and I don't know how things work). the error is
Assets/Scripts/InputManager.cs(23,34): error CS1061:
'PlayerInput.OnFootActions' does not contain a definition for
'Movement' and no accessible extension method 'Movement' accepting a
first argument of type 'PlayerInput.OnFootActions' could be found (are
you missing a using directive or an assembly reference?)
I get this same error for three elements in my code, which is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class InputManager : MonoBehaviour
{
private PlayerInput playerInput;
private PlayerInput.OnFootActions onFoot;
private PlayerMotor motor;
// Start is called before the first frame update
void Awake()
{
playerInput = new PlayerInput();
onFoot = playerInput.OnFoot;
motor = GetComponent<PlayerMotor>();
}
// Update is called once per frame
void FixedUpdate()
{
//tell the player motor to move using the value from our movement action
motor.ProcessMove(onFoot.Movement.ReadValue<Vector2>());
}
private void OnEnable()
{
onFoot.Enable();
}
private void OnDisable()
{
onFoot.Disable();
}
}
Does anyone have any idea on how to solve this? Thank you in advance, please be patient as I don't have much experience! ^_^
I followed the tutorial step by step but I still get problems :(

Setting a bool active from a different script

I need to active icons in my UI. Since I make use of a pick up script I want to activate the object once I picked it up. I made a bool in my main script but I can't turn the bool on or off in my pick up script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NadePickUp : MonoBehaviour
{
public GameObject PUEffect;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
PickUp(other);
// activate = true;
}
}
void PickUp(Collider2D player)
{
Shooting stats = player.GetComponent<Shooting>();
stats.shotType = "grenade";
GameObject effect = Instantiate(PUEffect, transform.position, Quaternion.identity);
PlayerStats activate = player.GetComponent<PlayerStats>();
activate = true;
Destroy(gameObject);
}
}
There are many answers to something like this on Unity Answers, here for example is one that answers your question.
If your PlayerStats is a class, you're treating it as a boolean. You're getting a reference to the script component and then immediately setting that value to true.
Assuming there's a public boolean activate on your PlayerStats class you could do something like this:
PlayerStats playerStats = player.GetComponent<PlayerStats>();
playerStats.activate = true;
This is based on pure assumption of your PlayerStats class.

I dont get why unity gives a compile error here

I'm trying to change the text of a TMPro text box, but unity reports at 20,79 I need a }. I put in the bracket and 11 errors pop up. debug pls? I don't know much c# myself so I can't do it myself. pls, help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class tetxmeshpro : MonoBehaviour
{ int previoushealth = 100;
private TextMeshProUGUI textMesH;
// Start is called before the first frame update
void Start()
{
var Player; GameObject;
textMesH = GetComponent<TextMeshProGUI> ();
}
// Update is called once per frame
void Update()
{
if ((Player.GetComponent(playerScript).Health)) not == previoushealth;)
{
previoushealth = Player.GetComponent(playerScript).Health;
textMesH = previoushealth;
}
}
}
What exactly is
var Player; GameObject;
supposed to do? ;)
It seems you rather mean
GameObject Player;
Either way the Player is not a field so later in Update you don't have access to it either. You probably rather wanted a
[SerializeField] private GameObject Player;
on class level so you can reference your object in it. Or even better give it the correct type right away:
[SerializeField] private playerscript player;
This way you don't need the GetComponent later.
What is this
if ((Player.GetComponent(playerScript).Health)) not == previoushealth;)
This is c# not some Shell/Dos script -> use != and not not ==. Also if conditions don't end with a ; and there is a ) too much. And also GetComponent either is generic, then it is GetComponent<T>() or you pass in a type but then you have to cast it like ((T)GetComponent(typeof(T))).
If something it should rather simply be
if (Player.GetComponent<playerScript>().Health != previoushealth)
Your class should rather look like
public class tetxmeshpro : MonoBehaviour
{
int previoushealth = 100;
// Link these via the Inspector using drag&drop
[SerializeField] private TextMeshProUGUI _textMesH;
[SerializeField] private playerscript _player;
// Start is called before the first frame update
private void Start()
{
if(!_textMesH) _textMesH = GetComponent<TextMeshProGUI>();
}
private void Update()
{
if (_player.Health != previoushealth)
{
previoushealth = _player.Health;
textMesH = previoushealth;
}
}
}
Though, note that naming your class textmeshpro a) might lead to confusions with the existing TextMeshPro and also doesn't at all reflect the purpose of that class. I would suggest something like e.g. HealthDisplay or anything similar meaningful.

Unity Videoplayer how to pause and play

Im trying to make a Vuforia Video Player with virtual buttons but when I try to pause and play it gives me and error. I looked at some forums and some question that is old but they didnt fix my problem. Error is:
Assets\vp_time.cs(23,9): error CS0120: An object reference is required for the non-static field, method, or property 'VideoPlayer.Pause()'
Assets\vp_time.cs(27,9): error CS0120: An object reference is required for the non-static field, method, or property 'VideoPlayer.Play()'
Code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using Vuforia;
public class vp_time : MonoBehaviour
{
public GameObject vbBtnObj;
public GameObject vbVpObj;
// Start is called before the first frame update
void Start()
{
vbBtnObj = GameObject.Find("VideoBtn");
vbBtnObj.GetComponent<VirtualButtonBehaviour>().RegisterOnButtonPressed(OnButtonPressed);
vbBtnObj.GetComponent<VirtualButtonBehaviour>().RegisterOnButtonReleased(OnButtonReleased);
vbVpObj.GetComponent<VideoPlayer>();
}
public void OnButtonPressed(VirtualButtonBehaviour vb){
VideoPlayer.Pause();
}
public void OnButtonReleased(VirtualButtonBehaviour vb){
VideoPlayer.Play();
}
// Update is called once per frame
void Update()
{
}
}
Well as the error says VideoPlayer is a type. It has no static method VideoPlayer.Play. You need a reference to an instance of type VideoPlayer and call the method on that instance.
vbVpObj.GetComponent<VideoPlayer>();
this line does absolutely nothing!
You rather wanted to store this reference in a field
// Drag this already in via the Inspector in Unity
[SerializeField] private VideoPlayer videoPlayer;
or do
private void Start ()
{
...
// As fallback get the reference on runtime ONCE
if(!videoPlayer) videoPlayer = vbVpObj.GetComponent<VideoPlayer>();
}
and later
videoPlayer.Play();
and
videoPlayer.Pause();

What do I do to solve an enclosing type error?

I have trying to create this code so that my player in Unity walks through a trigger box and sets off specific audio but when I put in this code it comes up with saying there's an Enclosing Error:
[10:51:37] Assets/DoorFall.cs(7,24): error CS0542: 'DoorFall': member names cannot be the same as their enclosing type
I have tried changing different names and moved different nodes around but nothing is working.
public class DoorFall : MonoBehaviour {
public AudioSource DoorFall;
// Use this for initializatoin
void Start() {
DoorFall = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update() {
}
void OnCollisionEnter (Collision collision) {
if (collision.gameObject.tag == "player")
{
DoorFall.play();
Destroy(collision.gameObject);
}
}
}
Your class name DoorFall is the same as a member of the class public AudioSource DoorFall; name one differently.
Chances are you dont need the member to be public either.
public class DoorFall : MonoBehaviour {
private AudioSource doorFallAudioSource;
// Use this for initializatoin
void Start() {
doorFallAudioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update() {
}
void OnCollisionEnter (Collision collision) {
if (collision.gameObject.tag == "player")
{
doorFallAudioSource.play();
Destroy(collision.gameObject);
}
}
}
In your case Compiler error CS0542 is being caused because the type name DoorFall is the same as the name of one of it's members (the public field). You should rename either the member or the type.
This generates a compile time error because the names conflict. Inside a method body in your type if you write or DoorFall.[memberHere] the compiler does not know whether to resolve the type or the member. Here is a more succinct example illustrating why this should generate a compile time error.
public class DoorFall {
private static string ToString() { return "A"; }
public AudioSource DoorFall = "B";
void Start() {
// what value would be assigned here?
var result = DoorFall.ToString();
}
}
On a side note it is almost never acceptable to declare a public field. Instead you should use properties to expose field values or to set them. If you want the value to be set/get publicly you could use an auto getter/setter.
public AudioSource DoorFall { get; set; }
If it never accessed from outside the type then declare it as private instead.

Categories

Resources