I have script #1 (store) which is working perfectly UNTIL I try to call script #2 (saver)
Maybe I am not thing this through correctly but When the player is leaving the store all I want to do is save the user info. As this info will be saved in other areas as well as when they exit I don’t want to put the 200 lines in each script. All I want to do is call Saver when the user clicks on the “leave store” button.
if(GUI.Button(new Rect(30,70,140,25),"Leave Store"))
{
Saver.Start(); //Here is where I want it to call the Saver.cs
Application.LoadLevel(TownScene);
}
This gives me
as an error. What am I missing?
Thanks in advance
PS Both Scripts are added to the same Object (Store).
use this to get the script, then call a function after the last “.” or use a variable or whatever you want. Those need be public elements, else you can’t access them. Also the “Start()” function will be run when the game starts/the object appears in the game, so there shouldn’t be a need to re-call it, use a custom function with the needed stuff instead.
Like:
void Start()
{
SaveGame();
}
void SaveGame()
{
//do your save actions here and then call "SaveGame()" from other scripts
}
My “saver” works perfectly. However now I want to call it from another scene.
If I put : gameObject.GetComponent(). to where I want to do another save in another scene I get: NullReferenceException: Object reference not set to an instance of an object.
If I add it to the scene I get an error on the server of : Could’t invoke RPC function ‘Saver’ because the networkView ‘SceneID: 2 Level Prefix: 0’ doesn’t exist.
When you load up the new scene are you saving or re-instantiating the object that the saver script is attached to?
if you are and you do not want to try using
DontDestroyOnLoad(transform.gameObject);
On the script/gameobject you want to carry from scene to scene.
Also
gameObject.GetComponent<Saver>();
Only works if you are trying to make that call from a script that is attatched to the same game object
If you are trying to call on Script:Saver attatched to GameObject: MainCamera
From a script attached to a random Cube in your game you would get that error.
What that does is declares the variable glob as a private variable of Global (bad example the script i am looking for is named Global.cs)
then when void Start is run it finds the gameobject ScriptHandler and then sets glob = the component script Global.
Which allows me to access any public vars/functions with glob.dosomething() or glob.varname