I can't figure out how to change the sprite used for the source image.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class SceneButton : MonoBehaviour
{
public Sprite ButtonOff;
public Sprite ButtonOn;
private void OnMouseDown()
{
gameobject.GetComponent<Image>().sprite = ButtonOn;
}
}
when I type this out it returns:
Assets\Scripts\SceneButton.cs(13,31): error CS1061: 'Image' does not contain a definition for 'sprite' and no accessible extension method 'sprite' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)
I have seen many posts where people use .sprite on an image component, so I am not sure why I am not able to
I think you should add using UnityEngine.UI; namespace. If it not work then try to make a public function as this:-
public GameObject soundButton;
public sprite soundOn;
public sprite soundOff;
public void ChangeSprite()
{
// getting Image component of soundButton and changing it
soundButton.GetComponent<Image>().sprite = soundOn;
}
And call it from the onclick of unity
May it's work for you
Related
I'm making a basic unity 2d c# and im making a system that detects what tool your holding I'm getting only two errors but they are very weird, they are: "Assets\clicked.cs(7,20): error CS0246: The type or namespace name 'spriteRenderer' could not be found (are you missing a using directive or an assembly reference?)" and "Assets\clicked.cs(7,20): error CS0246: The type or namespace name 'spriteRenderer' could not be found (are you missing a using directive or an assembly reference?)
"
here's the code
using UnityEngine;
using UnityEngine.UI;
public class clicked : MonoBehaviour
{
public Image Image;
player sprite; spriteRenderer.sprite
void Start()
{
image = GetComponent<Image>();
String tool;
image.gameObject.AddComponent<Button>().onClick.AddListener(HandleClick);
}
void HandleClick()
{
tool = sprite.name;
}
}
Your code should be:
public Image Image;
//This line changed
SpriteRenderer spriteRenderer;
void Start()
{
image = GetComponent<Image>();
String tool;
image.gameObject.AddComponent<Button>().onClick.AddListener(HandleClick);
}
void HandleClick()
{
tool = spriteRenderer.sprite.name;
}
You used spriteRenderer instead of SpriteRenderer
and the declaration of the sprite was weird... (not how you need to do it)
I got this error and I can't find a way to fix it
here is the error:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class InputManager : MonoBehaviour
{
private PlayerInput playerInput;
private Playerinput.OnFootActions onFoot;
void Awake()
{
playerInput = new PlayerInput();
onFoot = playerInput.OnFoot;
}
void Update()
{
}
private void OnEnable()
{
onFoot.Enable();
}
private void OnDisable()
{
onFoot.Disable();
}
}
'PlayerInput' does not contain a definition for 'OnFoot' and no accessible extension method 'OnFoot' accepting a first argument of type 'PlayerInput' could be found (are you missing a using directive or an assembly reference?) [Assembly-CSharp]
General note: Unity InputSystem package already brings its own type called UnityEngine.InputSystem.PlayerInput.
=> Are you sure, you are referencing the correct type?
If I
replicate your settings in a new project
make sure the Generate c# class is enabled
make sure to hit Save Asset
make sure the correct PlayerInput type is referenced
then I can access
var playerInput = new PlayerInput();
var onFoot = playerInput.OnFoot;
I'm just trying to edit a Button's text on my Unity app whenever the game loads.
ArgumentException: GetComponent requires that the requested component 'Text' derives from MonoBehaviour or Component or is an interface.
Below is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.UI;
internal class Text
{
internal string text;
}
public class Backend : MonoBheaviour
{
void Start()
{
GameObject.Find("Option 1 Button").GetComponentInChildren<Text>().text = "Sdfsdfsd";
PopulateHeadlines();
}
What am I doing wrong?
Your definition of internal class Text hide the UnityEngine.UI.Text.
Delete your definition and try again.
I am using unity's new input system and getting this error, I tried a lot to fix it but can't find the problem. Please help me.
Error:
Assets\Player01.cs(4,32): error CS0234: The type or namespace name 'Input' does not exist in the namespace 'UnityEngine.Experimental' (are you missing an assembly reference?)
Full Script(C#):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.Input;
public class Player01 : MonoBehaviour
{
public InputMaster controls;
void Awake ()
{
controls.Player01.Shoot.performed += _ => Shoot();
}
void Shoot ()
{
Debug.Log("We shot the sherif!");
}
private void OnEnable()
{
controls.Enable();
}
private void OnDisable()
{
controls.Disable();
}
}
The problem is that there is no namespace of UnityEngine.Experimental.Input. But, it only says “'Input' does not exist in the namespace 'UnityEngine.Experimental'”. ‘Experimental’ does exist and ‘Input’ does not. However, ‘InputSystem’ does. And that is the one you are looking for, not ‘Input’. You should change the first line to this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.InputSystem;
public class Player01 : MonoBehaviour
...
Just in case, if the first solution did not work, Close vs studio / code if open, then go to Edit -> Project Settings -> Input System Manager. There will be only one option available. Click the button, save your scene. Open a script. This worked for me. A lot of tutorials do not mention this part.
I'm trying to change text in My_Text but it doesn't work.
I'm using UnityEngine.UI.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class notbugdText : MonoBehaviour {
public Text My_Text;
// Use this for initialization
void Start () {
My_Text.text = "Hello world!";
}
// Update is called once per frame
void Update () {
}
}
I've got error message: Assets/scripts/notbugdText.cs(11,11): error CS1061: Type 'Text' does not contain a definition for 'text' and no extension method 'text' of type 'Text' could be found. Are you missing an assembly reference
I'm using MonoDevelop-Unity to program in C#
Converting my comment: if you have a class of yours named Text, it will override Unity's one. In order to differentiate that, you should declare My_Text as:
public UnityEngine.UI.Text My_Text;
and using UnityEngine.UI can be omitted, at this point.