Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by kevinamoin23 · Sep 09, 2016 at 05:03 PM · getcomponentfunctions

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'"
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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..

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kevinamoin23 · Sep 09, 2016 at 05:53 PM 0
Share

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.

avatar image flaviusxvii · Sep 09, 2016 at 06:47 PM 0
Share

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.

avatar image kevinamoin23 flaviusxvii · Sep 09, 2016 at 06:55 PM 0
Share

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 :(

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

52 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Calling a function in a prefab from a function of another GameObject in C#,Calling a function in a prefab from a function in another GameObject in C# 0 Answers

is gameObject.Getcomponent an expensive call? 3 Answers

Detecting for a variable from multiple potential scripts. 1 Answer

Problem with reading values from scripts on GameObjects in Lists 1 Answer

Whats the difference between GetComponent and (Player)FindObjectOfType() ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges