Hello. I want to call a function on another script but a got an error how can I fix this :'( Im a beginner please help. The Script name that I want to call its function is Save Manager.
public bool enter;
public GameObject SaveMessage;
void Start () {
enter = false;
SaveManager Autosave = GetComponent<SaveManager>().SaveState(); // this line tells about "cannot implicitly convert type 'void' to 'Save Manager'"
}
Answer by flaviusxvii · Sep 09, 2016 at 05:38 PM
Does SaveState() return void? I'm guessing it does, cause whatever SaveState() returns is being assigned to your variable 'Autosave', which is of type SaveManager. I'm guessing you don't want to do that assignment. So just do:
GetComponent<SaveManager>().SaveState();
Without assigning the result to SaveManager Autosave..
Ok sir thanks :) but I have another question the reason why I need to get the component of the script is because I want to save my game on a trigger like a Autosave. maybe I can show you the script of the Save$$anonymous$$anager. Im confused on doing that sorry.
using UnityEngine; using System.Collections; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO;
public class Save$$anonymous$$anager : $$anonymous$$onoBehaviour {
public void SaveState()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/PlayerData.dat");
PlayerData data = new PlayerData();
data.posx = transform.position.x;
data.posy = transform.position.y;
data.posz = transform.position.z;
data.rotx = transform.eulerAngles.x;
data.roty = transform.eulerAngles.y;
data.rotz = transform.eulerAngles.z;
bf.Serialize(file, data);
file.Close();
}
public void LoadState()
{
if(File.Exists(Application.persistentDataPath + "/PlayerData.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/PlayerData.dat", File$$anonymous$$ode.Open);
PlayerData data = (PlayerData) bf.Deserialize(file);
file.Close();
transform.position = new Vector3(data.posx, data.posy, data.posz);
transform.rotation = Quaternion.Euler(data.rotx, data.roty, data.rotz);
}
}
[Serializable]
class PlayerData
{
public float posx;
public float posy;
public float posz;
public float rotx;
public float roty;
public float rotz;
}
} This is the code for the Save$$anonymous$$anager Script. and I will show you also the code for the SaveTrigger in which getting the component of the function of the script "Save$$anonymous$$anager"
using UnityEngine; using System.Collections;
public class SaveTrigger : $$anonymous$$onoBehaviour {
public bool enter;
public GameObject Save$$anonymous$$essage;
void Start () {
enter = false;
GetComponent<Save$$anonymous$$anager>().SaveState();
}
void Update () {
if(enter)
{
Save$$anonymous$$essage.SetActive(true);
}
if(!enter)
{
Save$$anonymous$$essage.SetActive(false);
}
}
void OnTriggerEnter(Collider col)
{
if(col.tag == "Player")
{
enter = true;
}
}
void OnTriggerExit(Collider col)
{
if(col.tag == "Player")
{
enter = false;
Destroy(gameObject);
}
}
} This is the code for a trigger and I cant do that for almost 3hrs :'( Im stuck please help. Thanks for reading my code its too long sorry.
As written, this will save only once when the gameobject is created. You can copy GetComponent<Save$$anonymous$$anager>().SaveState();
to anywhere else you want the state to be saved. It's a little inefficient to use GetComponent, but i don't think it would be happening excessively.
Thanks for the response. Im not going to save the game on a trigger I changed my $$anonymous$$d. it so difficult to me to do it but how can I do the load game in the menu when the save is actually in a gameplay. loading the specific scene and the position of the player because all I can do is the exact location of the player in the scene with the save and load button on the same scene. I want to do the load button to be on the main menu which is another scene how can I do that :'( Please help :(