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
Related
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();
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
the stuff at the top is just stuff unity adds i am just adding this because my post has too much code apparently
void Update()
{
This tells it to print hi in the console
if (Input.GetKeyDown(KeyCode.Space)) ;
{
Debug.Log("hi.");
}
}
}
hmm, may be your problem is in your code, just clear the ; element
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("hi.");
}
}
The compiler will show you a red error and also in unity editor.
If you have done my instruction but still can not do it, create a gameobject in the hierarchy and drag the script you have written to the gameobject
I'm trying to change the Text of an UI GameObject with a script but Unity doesn't let me drag the GameObject with the text on the script reference and when i do public GameObject it says GameObject is not a Text.
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
using UnityEngine.UI;
public class GhostName : MonoBehaviour
{
public Text NameText;
void Start()
{
NameText.text = "The Name is : ";
}
}
You need to store NameText as a reference to a GameObject, and then call GetComponent<Text>() to get the Text component on the object. This way, you'll be able to drag the game object into the 'NameText' field in the Unity Inspector window.
...
public class GhostName : MonoBehaviour
{
public GameObject NameText;
void Start()
{
NameText.GetComponent<Text>().text = "The Name is : ";
}
}
You should also check that GetComponent<Text>() is not equal to null before setting the value of .text.
If anyone still get this issue, I had to change from "TextMeshPro" to "Text" Object.
Disclaimer: I am totally new to Unity. But it seems I have the same issue.
Under Hierarchy -> right click -> UI, there is only "Text - TextMeshPro" option in my Unity version (2021.3.4f1). I don't know why "Text" is missing. (anyone concur with me?)
So I fail to follow this step in a beginner tutorial, I anyway just select TextMeshPro instead. Then I just cannot drag to script. I guess it is because the tutorial is using Text type not TextMeshPro and they are not compatible.
Later I realize that, to enable the drag action, one cannot use [SerializeField] Text xxx; but must use [SerializeField] TextMeshProUGUI xxx;.
Pls also include using TMPro; in the header.
I used TMPro namespace and public TextMeshProUGUI scoreText;
and I was able to drag and drop the Text (TMP) to the script as below:
using TMPro;
using UnityEngine;
public class className : MonoBehaviour
{
// Start is called before the first frame update
public TextMeshProUGUI scoreText;
void Start()
{
scoreText.text = "Score: "+ points;
}
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.
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