How to change text in Unity Text - c#

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.

Related

I have tried using a public void function from a different script and error message CS1061 pops up

I am making a game in Unity with dialogue boxes and have one script made for putting the dialogue box on the screen and another to have the actual text put on the dialogue box.There is one function in the first script I have to use in the second to make it work. The problem however is that when I try to reference the first script it always occur an error.
The error message is:
Assets\scripts\dialogueHolder.cs(29,18): error CS1061: 'dialogueManager' does not contain a definition for 'ShowBox' and no accessible extension method 'ShowBox' accepting a first argument of type 'dialogueManager' could be found (are you missing a using directive or an assembly reference?)
First code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class dialogueManager : MonoBehaviour
{
public GameObject dialogueBox;
public Text dialogueText;
public bool activeDialogue;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (activeDialogue == true && Input.GetKeyDown(KeyCode.Return))
{
dialogueBox.SetActive(false);
activeDialogue = false;
}
}
public void ShowBox(string lines)
{
activeDialogue = true
dialogueBox.SetActive(true);
dialogueText.text = lines;
}
}
Second code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class dialogueHolder : MonoBehaviour
{
public string dialogue;
private dialogueManager dMan;
public GameObject Manager;
// Start is called before the first frame update
void Start()
{
dMan = Manager.GetComponent<dialogueManager>();
}
// Update is called once per frame
void Update()
{
}
void OnTriggerStay2D(Collider2D other)
{
if(other.gameObject.name=="Shadow")
{
if(Input.GetKeyDown(KeyCode.Return))
dMan.ShowBox(dialogue);
}
}
}
I can clearly see that the function ShowBox I am trying to access from the first script is the problem. But I can't figure out what to do with the code to make it work. I have tried different methods to try and reference the Class dialogueManager. For an example I have tried refrencing the actual gameobject in the game or the script alone but I can't seem to get it to work. If I remove the function ShowBox the code will run but I need the ShowBox function to actually make the dialogue boxes in my game to work so that is not a solution.
"activeDialogue = true" maybe you should put a ";" after this

Changing text of a Button in Unity upon loading

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.

The type or namespace name 'Input' does not exist in the namespace 'UnityEngine.Experimental'

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.

How do I change the source image of a Unity image component?

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

Cannot write my code right, because of an error CS0111

I'm a beginner in programming, So i don't know how to fix this error.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ZombieScript : MonoBehaviour
{
public Transform player;
public Transform zombie;
public GameObject zombieScript;
bool canActive = false;
void Start()
{
if (canActive == false) { zombieScript.SetActive(true); }
}
void Update()
{
UnityEngine.Debug.Log(Mathf.Abs(Vector2.Distance(player.transform.position, zombieScript.transform.position)));
if (Mathf.Abs(Vector2.Distance(player.transform.position, zombieScript.transform.position)) <= 10.00000 && canActive == false)
{
zombieScript.SetActive(true);
canActive = true;
}
}
}
Unity writes:
Assets\ZombieScript.cs(5,14): error CS0101: The namespace '' already contains a definition for 'ZombieScript'
Assets\ZombieScript.cs(12,10): error CS0111: Type 'ZombieScript'
already defines a member called 'Start' with the same parameter types
Assets\ZombieScript.cs(17,10): error CS0111: Type 'ZombieScript'
already defines a member called 'Update' with the same parameter types
in advance, thank u <3
A basic structure of a C# class looks like this:
using ...
namespace CompanyXYZ.ProductABC
{
public class Entity
{
// Constructors, properties, methods & etc.
...
}
}
CS1011 compiler error will be thrown whenever it found more than one definition within the same namespace.
Namespace allows us to avoid conflict of classes with the same name by grouping them in different namespaces.
In this case, you probably have more than one ZombieScript defined under the same namespace which is not shown in your code. Therefore, kindly check if you have it defined somewhere else (within the same namespace) and if found it you're good to go!

Categories