I have 2 different scenes.In the first scene i have a script that has my variables.In the second scene i’m trying to call the instance of that object(that the script is) and i get the following error
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class statsTextsToDisplayAndChanges : MonoBehaviour
{
public static statsTextsToDisplayAndChanges _INstance;
public Text m_LevelText;
public Text m_AttackText;
public Text m_DefendText;
public Text m_MoneyText;
public int level = 0;
public int money = 400;
public int health = 400;
public int attack = 10;
public int def = 5;
string answer;
string url = "http://alex3685.dx.am/display.php";
public void Start()
{
m_LevelText.text = level.ToString();
m_AttackText.text = attack.ToString();
m_DefendText.text = def.ToString();
m_MoneyText.text = money.ToString();
_INstance = this;
DontDestroyOnLoad(this.gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class buyWeapons : MonoBehaviour {
statsTextsToDisplayAndChanges st;
public int sword1 = 200;
private int sword2 = 300;
private int sword3 = 400;
private int sword4 = 600;
private int sword5 = 750;
private int sword6 = 900;
public void OnMouseDown()
{
if (statsTextsToDisplayAndChanges._INstance.money >= sword1)
{
Debug.Log("YOU BOUGHT IT");
}
else
{
Debug.Log("YOU CAN'T BUY IT");
}
}
}
The error is in the second script here
if (statsTextsToDisplayAndChanges._INstance.money >= sword1)
Yeah i added the script on playerstats and it didn’t work.The thing is i have exactly the same “code” (but of course with two other scenes and gameobjects) and it works fine…
Have you recently deleted any of the scripts attached to an object in the project folder? without removing them from the object itself first, sometimes it gives that error, if that’s the case then it should just sit there as an empty script component.
ps. didn’t read any of the scripts that you have written, but majority of times when i get that error it’s the components on a gameobject causing it, and mainly the empty scripts that i forget to delete in the inspector, hope it helps, otherwise just ignore me.
I would never ignore anyone,especially if he wants to help me!I can’t remember if i deleted anything but i think i didn’t.I double checked just now and everything seems to be ok(according to my view of course ).But hey,thanks for trying to help me
so i just read through your code, so first off there’s one thing i don’t really get in terms of the way you build the code, so you reference the script in the second script, however(now i may be wrong) the script does not carry over from the first scene(at least not in the hierachy, again, might be wrong as it may load due to the dontdestroyonload as seen in the first scene), but if i am right then you essetially try to reference a script that does not exist in the current scene, and therefore it can’t find a reference.
below is some nonsense which just didn’t look correct to me, in the setup of the code, feel free to skip.
also another thing that’s kinda odd is that you use the _instance variable to set it to itself, to me that seems kinda counterproductive as you basically make a fresh instance of the script(which if things were working should be carried over, and therefore not have a need to be duplicated), where as you could make a new object in scene 2 with that script instead and just draw reference directly to the script, making changing values and such a lot easier.
what im trying to say is that you could make it easier by either importing the stats from a json file or such(the while money, health and such class), or focus on bringing it over from the first scene and then using those values directly, as making a new instance of the script just isn’t necessary.
again, i might be wrong and the script carries over perfectly once you load the game, i just cant really see if that’s the case since there’s no picture of it.
hopefully you can find some of my babbling useful, otherwise sorry for wasting time.
I will show you my other example of two different scenes that i did the same thing and it works
First scene:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class internetCalls : MonoBehaviour
{
public static internetCalls _Instance;
public bool IsLoggedIn = false; // these you can access from outside the instance to make sure the user logged in
public string LoggedInUsername = ""; // or to get the username
public string message = "";
public displayDataFromPhp display;
public bool IsSamurai = false;
void Start()
{
_Instance = this;
DontDestroyOnLoad(this.gameObject);
}
public void OnGUI()
{
if (message != "")
GUILayout.Box(message);
}
public void DoLogin(string user, string password)
{
IsLoggedIn = true;
IsSamurai = true;
LoggedInUsername = user;
Debug.Log("Logged in: " + user);
UnityEngine.SceneManagement.SceneManager.LoadScene("map");
etc etc etc.....
}
And the second scene:
public class loadImage : MonoBehaviour {
string urlknight = "https://s-media-cache-ak0.pinimg.com/736x/5f/58/07/5f580731263a6f95f1268cc681e6ac04.jpg";
string urlsam = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0e/Samurai.jpg/220px-Samurai.jpg";
Texture2D img;
internetCalls sam;
// Use this for initialization
void Start () {
if (internetCalls._Instance.IsSamurai == true)
{
StartCoroutine(LoadImgSam());
}
else
{
StartCoroutine(LoadImgKnight());
}
//StartCoroutine(LoadImgKnight());
}
etc etc etc.....
Sorry for meshing with you but i’m new to all this(unity-coding) and maybe some of my questions-answers are stupid
I agree the static instance variable makes no sense unless you use the singleton pattern. This is not a singleton though since nothing is preventing several instances from being made.
Look up examples of Singleton’s used in Unity for examples.
i mean, it is basically the same code, i agree with that, and i guess by the ‘it works’ part you mean it does load the right road map and all, one thing i don’t see in the first post though is the part where you transition to the new scene, this is gonna be another shot in the dark but have you made sure that the dontdestroyonload is located in the right spot?
eg. DontDestroyOnLoad(transform.gameObject) anywhere or this.gameObject in the topmost object in the hierachy(from where you want to carry over the object), don’t know if that might be something, can’t say i’ve ever felt the need to use dontdestroyonload as i prefer just pasting stuff to a savefile(joy of singleplayer games), but maybe .
anyways, i cant really come up with anything else, because if you say that the other scripts work, which are identical in structure(more or less), then the only cause that i see is something to do with something else from the playerstats object in the scene not being imported properly into the new scene.
Hey, I think the problem you have is with your method name. OnMouseDown() is a built-in method within Unity used for GUI components but it expect a trigger/collider or a raycast around the GUI element. Try renaming the method to something like “OnPurchase()” and see if that works.