Changing text of a Button in Unity upon loading - c#

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.

Related

scrip class can't be found for adding c# code to unity for loading scenes

I am attempting to add a c# script to python allowing me to use UnityEngine.SceneManagement to load a scene of a game for a school assignment I am completing, but whenever I try to add the code to unity I get an error message stating "Can't add script component 'menu control' because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match". I am relatively new to C# and wouldn't be surprised if there was just some small error in my code I couldn't spot. Here is my code below
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class MenuController : MonoBehaviour
{
[Header("Levels to load")]
public string _newGameLevel;
private string levelToLoad;
[SerializeField] private GameObject noSavedGameDialogue = null;
public void newgameYes()
{
SceneManager.LoadScene(_newGameLevel);
}
public void loadgameYes()
{
if (PlayerPrefs.HasKey("Savedlevel"))
{
levelToLoad = PlayerPrefs.GetString("SavedLevel");
SceneManager.LoadScene(levelToLoad);
}
else
{
noSavedGameDialogue.SetActive(true);
}
}
public void QuitButton()
{
Application.Quit();
}
}

Function not showing up on unity button

I am learning Unity and C#. I am trying to use a button to trigger something, but the function is not showing up. Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExitButton : MonoBehaviour
{
public void OnButtonTrigger () {
Debug.Log("Application Quitting");
Application.Quit();
}
}
And here is a screenshot of where the problem is:
attach that script to a game object as a component then drag that object to OnClick and you will find the functions.

Why am I getting error on TryGetSettings when using postprocessing v2 ? And how can I fix it?

In the editor on my Camera I added two components : Post Process Layer and Post Process Volume
On the first component the layer I changed the Layer to a layer I added PostProcessing
On the second component I set the Is Global to true and added one effect for now Depth Of Field
Now I want to change and set values to the Depth Of Field Focal Length via a script.
So I created a new script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
public class PostprocessingEffects : MonoBehaviour
{
private PostProcessVolume postProcessVolume;
private DepthOfField depthOfField;
void Start()
{
postProcessVolume = GetComponent<PostProcessVolume>();
postProcessVolume.profile.TryGetSettings(out PostProcessEffectSettings depthOfFieldSettings);
depthOfField.focalLength.value = 1f;
}
}
I'm getting error on the line :
postProcessVolume.profile.TryGetSettings(out depthOfField);
On the TryGetSettings and the error is :
The type 'PostprocessingEffects' cannot be used as type parameter 'T' in the generic type or method 'PostProcessProfile.TryGetSettings(out T)'. There is no implicit reference conversion from 'PostprocessingEffects' to 'UnityEngine.Rendering.PostProcessing.PostProcessEffectSettings'.
Tried also :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
public class PostprocessingEffects : MonoBehaviour
{
private PostProcessVolume postProcessVolume;
private DepthOfField depthOfField;
void Start()
{
postProcessVolume = GetComponent<PostProcessVolume>();
postProcessVolume.profile.TryGetSettings(out DepthOfField depthOfField);
depthOfField.focalLength.value = 1f;
}
}
It seems you're using incorrect type.
TryGetSettings cannot take type of PostprocessingEffects
I believe you want DepthOfField try:
postProcessVolume.profile.TryGetSettings(out DepthOfField depthOfField);
of if you have your variable already defined:
private DepthOfField depthOfField;
postProcessVolume.profile.TryGetSettings(out depthOfField);
Also make sure you have depth of field effect attached to Postprocess Volume in inspector

How to pass data from one scene to another?

I want to take username through InputField in scene 1 and access the username entered by the user in scene 2. I have a InputField and a submit button in scene 1. When click the submit button, the scene changes. How should I go about it?
I tried using DontDestroyOnLoad() but this didn't worked for me.
To change the scene i am using SceneManager.LoadScene(name);
Here I came up with this.I achieved what you wanted with the using PlayerPrefs
I created a InputField and attached a script to it called Save and then I called SaveText() from that script after On End Event in inspector
Save.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Save : MonoBehaviour
{
InputField userNameField;
void Start()
{
userNameField = GetComponent<InputField>();
}
public void SaveText()
{
PlayerPrefs.SetString("userName", userNameField.text);
Debug.Log(userNameField.text);
}
public void MoveScene(string sceneToLoad)
{
SceneManager.LoadScene(sceneToLoad);
}
}
and in next Scene I attached the script called Load.cs to a text gameobject and in Start() I got information of that String that saved previously
Load.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Load : MonoBehaviour
{
Text userNameText;
void Start()
{
userNameText = GetComponent<Text>();
userNameText.text = PlayerPrefs.GetString("userName");
}
}
Note : I don't know how efficient this is, But it does the work

In unity C# script why Input is not exist?

using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (
}
}
Inside the if ( i type Input but it's not exist.
I tried to add in the top using.System.Io; but that's not the solution.
In my unity project i clicked in the menu on Assests > open C# Project and it opened a new mono develop scripting window.
In my unity project i have a firstpersoncharacter and under in it a spotlight.
I want to make in the script a key trigger like if i click on the key F it will turn on the spotlight and if i click on F again it will turn the spotlight off.
if there is no input you should check script:
1.fix compiler errors
2.if you haven't libraries you can not access to input or etc?
using UnityEngine;
using System.Collections;
3.if you can't access to input you should install new monodevelop version from monodevelop.com
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public bool mybool;
void Update() {
if (Input.GetKeyDown(KeyCode.F))
GetComponent<Light> ().enabled = !mybool;
}
}
explain:
!false = true
!true = false

Categories