I have this simple platformer that has coins that you pick up and a canvas that shows the score and changes whenever you pick one up. This is my code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
Text text;
private LVLMang levelManager;
void Start () {
text = GetComponent<Text> ();
levelManager = FindObjectOfType<LVLMang> ();
}
void Update () {
text.text = "" + levelManager.Score;
}
}
It will add in the coins to the score, but it gives me so many errors that my game eventually crashes. The error is: 'NullReferenceException: Object reference not set to an instance of an object' and it highlights the text.text line. Please Help. Thanks in advance.
First, make sure you have a Text component on the object that has the script. Your GetComponent<Text> (); call isn't finding a Text component.
Alternatively you can make Text text; public and hook it to the actual Text component you want to change by drag-dropping the Text-component object into the Script component's Text field.
Secondly, after updating the text.text value, call text.SetAllDirty(); to get the change to take effect.
Related
Before that, i apologize if my english is bad.
I made an AR about crystal structure. I will make an AR with 36 crystal structure objects with 9 markers/image target which means 1 marker has 4 objects to be displayed. The concept that I want to make is that these objects are connected to the spawn and destroy system and I managed to make it with the help of tutorials on youtube. but I have a problem, my AR object doesn't want to be on top of the marker/image tergets. if I try to adjust the position of the object, when I try to run it happens that the object is still next to the marker and follows the 0,0,0 coordinates of the object which is not above the marker, even though I have already inserted the object into the marker. I am very confused how to solve my problem.
this is part script for my project to spawn and destroy system:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sistem : MonoBehaviour
{
public static Sistem instance;
public int ID;
public GameObject TempatSpawn;
public GameObject[] KoleksiStruktur;
// Start is called before the first frame update
void Awake()
{
instance = this;
}
void Start()
{
SpawnObject();
}
public void SpawnObject()
{
GameObject Benda = Instantiate(KoleksiStruktur[ID]);
Benda.transform.SetParent(TempatSpawn.transform, false);
Benda.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
Debug.Log("Objek berhasil di-instantiate");
}
}
the object and the marker/image target
i tried to solve the problem by setting the object over the marker but still can't fix it. I hope to find a solution how I can display my object above the marker.
I am pretty new to Unity and game development so sorry if this is a stupid mistake.
I am trying to create a UI in which you enter text into a input field (TMP), the script will check the input and if it reads "password", a Debug.Log is shown in the console.
I have looked around online for any help with this but nothing seems to work.
I have got a script that I assume to work, but I am getting one major problem: On line 15 it cant find the Input Field that I am trying to reference?
The script is attached to the Input Field, I am trying to reference it from there.
Any help would be greatly appreciated... Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CheckPassword : MonoBehaviour
{
public InputField inputField;
public void Awake()
{
inputField = GetComponent<InputField>();
}
public void CheckInputField()
{
if (inputField.text == "password")
{
Debug.Log("Password Correct");
}
}
}
The problem was that due to the fact that I was using a Text Mesh Pro Input Field I had to specify that in the variable.
Instead I used: [SerializeField] TMPro.TMP_InputField inputField;
And the awake function was not necessary either.
I'm fairly new to Unity and have been looking up tons of tutorials/guides online. My issue is that for some reason when I use the below code it doesn't detect if the keyboard is clicked. Maybe I am doing keyboard detection wrong. Here is my code:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
Vector3 player = GameObject.FindGameObjectWithTag("Player").transform.position;
void Update () {
if (Input.GetKeyDown(KeyCode.D)) {
player.x += 0.01F;
}
}
}
Your input code is correct but still couple of things that are not at right place. First you wrote an initializer (static method) outside any function. Remember when you do it in Unity3d C# then it will always give you a warning/error.
If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function.
So first move that sort of lines in either functions.
Second thing you are getting Vector3 and trying to use it as reference, that means you got a position reference in form of Vector3 and every change made in that variable will be effective, that is not the case, it won't.
But yes you can do it by getting Transform or GameObject, they will do it for you.
Third and last thing, you are trying to alter Vector3 component ("x" in your case ) directly, that'd also not acceptable for Unity. What you can do is either assign position with new Vector3 or create a separate Vector3 variable, alter that, then assign it to position.
So after all of these addresses your code should be look like this,
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
Transform player;
// Use this for initialization
void Start ()
{
player = GameObject.FindGameObjectWithTag ("Player").transform;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown (KeyCode.D)) {
// Remove one of these two implementations of changing position
// Either
Vector3 newPosition = player.position;
newPosition.x += 0.01f;
player.position = newPosition;
//Or
player.position = new Vector3 (player.position.x + 0.01f, player.position.y, player.position.z);
}
}
}
I am new to unity and want to get a value from a input text field. I found this question
Get text from Input field in Unity3D with C#, but when I execute it the same error always appear: NullReferenceExcpetion : Object reference not set to an instance of an object.
It seems like a stupid mistake and I tried everything but can't seem to fix it.
My code:
void Start () {
var input = gameObject.GetComponent<InputField>();
input.onEndEdit.AddListener(SubmitName);
}
private void SubmitName(string arg0)
{
Debug.Log(arg0);
}
I tried putting InputField input; before the start function and erasing var but still no luck.
If anyone can help me with this problem it would be much appreciated.
Pictures of where my scripts are attached at the moment.
Your code is nearly correct, if you have a look at the documentation, you have to call your method with function before calling the method name. It looks to me like you are mixing c# and JS, here is the `js function:
public class Example {
public var mainInputField;
public function Start() {
// Adds a listener to the main input field
// and invokes a method when the value changes.
mainInputField.onValueChange.AddListener(function() {
ValueChangeCheck();
});
}
// Invoked when the value of the text field changes.
public function ValueChangeCheck() {
Debug.Log("Value Changed");
}
}
the c# solution:
public class MenuController : MonoBehaviour {
[SerializeField]
InputField inputText;
// Use this for initialization
void Start () {
inputText.onValueChange.AddListener(delegate {
DebugInput();
});
}
private void DebugInput(){
Debug.Log ("Input: " + inputText);
}
}
My Hierarchie looks like this:
I created a Canvas and inserted an InputField inside it. The script with the code inside is attached to the Canvas and the InputField connected to the [SerializeField] inside the script in the Canvas.
I would recommend you to make your InputField a class variable, so you can access it easier later. Furthermore you should just create a [SerializeField] for your InputField so you can drag it to your script. This might avoid some mistakes too.
Change InputField(TMP) to InputField(Legacy)
I'm making a level select screen and I need the text field to display different level numbers for each level. I don't really see what I'm doing wrong here, but I'll go over what I did and post the relevant code.
I have a button class (linked) and inside the symbol I have a dynamic text field. I have two classes of relevance, LevelSelectScreen and LevelSelectButtons (pretty self-explanatory what they are). I thought it would be really easy to change the text if I did it inside the LevelSelectButtons class, by simply doing levelText.text = "Wanted Text", where levelText is the given instance name for my button (just a text field on top of my graphic for the button). Unfortunately, this gives the oh so common and annoying error: TypeError: Error #1009: Cannot access a property or method of a null object reference.
I tried doing virtually the same thing in my LevelSelectScreen class during my loop, but I got the same error. Help on how to get this levelText to work is greatly appreciated! Here is the relevant code.
LevelSelectScreen
public class LevelSelectButtons extends SimpleButton {
public var levelNumber:int;
public var levelSelectScreen:LevelSelectScreen;
public function LevelSelectButtons(i) {
x = 200;
y = 100 + 50*i;
addEventListener(MouseEvent.CLICK,LevelSelectClicked,false,0,true)
levelNumber = i;
levelText.text = "Level" + i;
}
}
LevelSelectScreen
public class LevelSelectScreen extends MovieClip {
public var levelSelectButtons:LevelSelectButtons;
public var mainMenuButton:MainMenuButton;
public function LevelSelectScreen() {
for (var i:int = 1; i<=2; i++)
{
levelSelectButtons = new LevelSelectButtons(i);
addChild(levelSelectButtons);
}
}
}
You can't have a dynamic text field in a SimpleButton.
Annoying, I know.
Simple fix would be to have LevelSelectButton wrap a SimpleButton instead of extend it. Then your text field would be inside LevelSelectButton on top of a text-less SimpleButton. (Be sure to set mouseEnabled to false on the text field so it doesn't interfere with mouse events on the SimpleButton.
A more complex option would be to write your own custom button class.
It's not actually that difficult, but might be overkill for what you're trying to do here.
its because you haven't declared levelText variable and you are trying to access it, therefor Cannot access a property or method of a null object reference.