Why is my unity scene script not working? - unity3d

using a button to load a scene and iam just getting the error "Assets\Scripts\button_press.cs(15,59): error CS1503: Argument 1: cannot convert from 'UnityEngine.Object' to 'string'"
code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class button_press : MonoBehaviour
{
public Object sceneToLoad;
public void PlayGame ()
{
UnityEngine.SceneManagement.SceneManager.LoadScene(sceneToLoad);
}
public void QuitGame()
{
Debug.Log (" You have quit");
Application.Quit();
}
}

The LoadScene API expects either an integer referring to the scene number in the build, or a string referring to the name of the scene (generally preferred).
You should define your sceneToLoad field as a string rather than an Object.

Related

The game object does not have a script attached to it

- error message
After completing the bullet script operation, I tried to insert the script into Bullet Sprite in 2D Object format, but an error was displayed and the insertion was not possible. I tried turning off and on the editor because I thought it was a bug because I had the same file name and class name, but it didn't work. Do you happen to know about this problem? The editor version is 2021.3.11f1.
Bullet Script
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;
public class Bullet : MonoBehaviour
{
[SerializeField]
private float speed = 10f;
private IObjectPool<Bullet> pool;
void Update()
{
transform.Translate(Vector2.up);
}
public void SetPool(IObjectPool<Bullet> bulletPool)
{
Launcher.Instance.bulletPool = pool;
}
private void OnBecameInvisible()
{
Launcher.Instance.bulletPool.Release(this);
}
}
I turned off and on the editor and changed the class name because I thought it was different.

error CS0117: 'ScoreSystem' does not contain a definition for 'HighscoreKey'

i was just writing my script and this appeared while trying to test the game. heres my script
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
public class MainMenu : MonoBehaviour
{
[SerializeField] private TMP_Text HighscoreText;
private void Start()
{
int Highscore = PlayerPrefs.GetInt(ScoreSystem.HighscoreKey, 0);
HighscoreText.text = $"High Score: {Highscore}";
}
public void Play()
{
SceneManager.LoadScene(1);
}
}
please help me, tq
The error is very clear: it says that inside the Score System class, it did not find the HighscoreKey variable.
To make this work, if you created the ScoreSystem class yourself, you should add a static string variable called HighscoreKey.
Like this:
public static string HighscoreKey = “…”;
Good Work!

NullReferenceException using ScriptableObjects in Unity

I have setup a Scriptableobject in database called "Card" and am attempting to make a searchable list of those items in the scriptableobject. I attached CardDatabase script to an empty gameobject called _CardDatabase. I am not sure if I am attaching it incorrectly or what I have done wrong here.
I am still getting the following error:
NullReferenceException: Object reference not set to an instance of an object
CardDatabase.GetCardByID (System.String abbr) (at Assets/Scripts/CardDatabase.cs:33)
// CardDatabase.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class CardDatabase : MonoBehaviour
{
public CardList cards;
private static CardDatabase instance;
private void Awake()
{
if (instance = null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
public static Card GetCardByID(string abbr)
{
return instance.cards.AllCards.FirstOrDefault(i=> i.abbr == abbr);
}
}
// CardList.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Cards Database",menuName = "Cards Database")]
public class CardList : ScriptableObject
{
public List<Card> AllCards;
}

Unity saving inputfield

I am making a 2D platformer where I want the users inputted name to hover over there character.
This is what the menu screen looks like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class set_usernames : MonoBehaviour {
public InputField name;
public InputField second_name;
private string Bader_input_name;
private string Dwarf_input_name;
public void updatename(string name){
}
public void setget()
{
//Debug.Log (name.text);
Bader_input_name = name.text;
Dwarf_input_name = second_name.text;
//Application.loadedLevel("game");
Debug.Log ("name was " + Bader_input_name);
Debug.Log ("name was " + Dwarf_input_name);
SceneManager.LoadScene ("map_select");
}
}
This is the error message I get
Basically what that means is that you are trying to access an object that's not instantiated. I have no idea how are you trying to access the input but the easiest way is to make two public inputs in your UIController drag and drop them in Unity and access the Text property of input objects like so: myInput.Text.

Accessing struct list on Start() gives NullReferenceError

What in the name of god am I doing wrong here? Every time I run the game I get this: "NullReferenceException: Object reference not set to an instance of an object". I know what that means I just don't get why it is saying that?
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class Inventory : MonoBehaviour {
public static Inventory instance;
public List<InventoryItems> INVENTORY_ITEMS = new List<InventoryItems>();
void Awake(){
instance = this;
}
void Start(){
Debug.Log(instance.INVENTORY_ITEMS); // ERROR
Debug.Log(INVENTORY_ITEMS); // ERROR
}
}
[Serializable]
public struct InventoryItems
{
public string name;
}
This is how you should write this code:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class Inventory : MonoBehaviour {
public static Inventory instance;
public List<InventoryItems> INVENTORY_ITEMS;//do not initialize here
void Awake(){
instance = this;
INVENTORY_ITEMS = new List<InventoryItems>();//init here instead
}
void Start(){
Debug.Log(instance.INVENTORY_ITEMS); //no ERROR
Debug.Log(INVENTORY_ITEMS); //no ERROR
}
}
[Serializable]
public struct InventoryItems
{
public string name;
}
Added note:
The reason for this error is that the value of a public serializable member in monobehaviour is being read from the editor (inspector) overwriting any value being assigned to it prior to Awake.
I suggest either change the list accessor to internal, or make it a property, or keep it public but let inspector handle the initialization and adding the initial elements.
Added another note:
According to Programmer and Uri Popov, your original code most certainly will work on unity 5.4 and later.