- Home /
Save Camera Rotation ERROR (HELP!!!!)
I need to save my camera rotation (from MouseLook.cs) I use this script :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace UnityStandardAssets.Characters.FirstPerson
{
public class QuickSave : MonoBehaviour
{
public string fileName = "QuickSave";
[System.Serializable]
class PR
{
public float MRX;
public float MRY;
}
public void Save(){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = new FileStream(Application.persistentDataPath +"/"+ "SaveFile", FileMode.Create);
PR newData = new PR();
newData.MRX=GetComponent<MouseLook>().xRot ;
newData.MRY=GetComponent<MouseLook>().yRot ;
bf.Serialize(file, newData);
file.Close();
}
public void Load(){
if (File.Exists(fileName)){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = new FileStream(Application.persistentDataPath +"/"+ "SaveFile", FileMode.Open,FileAccess.Read);
PR newData1 = (PR)bf.Deserialize(file);
GetComponent<MouseLook>().xRot = newData1.MRX;
GetComponent<MouseLook>().yRot = newData1.MRY;
file.Close();
}
}
void LateUpdate () {
if (Input.GetKey (KeyCode.F5))
{
Save();
}
if (Input.GetKey (KeyCode.F9))
{
Load();
}
}
}
}
When I Save rotation I get ERROR: ArgumentException: GetComponent requires that the requested component 'MouseLook' derives from MonoBehaviour or Component or is an interface.
When I Load rotation I get ERROR: SerializationException: serializationStream supports seeking, but its length is 0
Answer by allenallenallen · Aug 07, 2015 at 04:19 PM
You need to declare the game object of where the MouseLook script is attached to.
Perhaps something like this:
public GameObject player; // Drag the game object that has MouseLook attached to it.
And add "player" right before all of your GetComponent.
For example:
newData.MRX = player.GetComponent<MouseLook>().xRot ;
player.GetComponent<MouseLook>().xRot = newData1.MRX;
It's not work
ArgumentException: GetComponent requires that the requested component '$$anonymous$$ouseLook' derives from $$anonymous$$onoBehaviour or Component or is an interface.
Well, where is $$anonymous$$ouseLook? Is it a component of an instance?
$$anonymous$$ouseLook - Standart assets script (Package : Character) Script ($$anonymous$$ouseLook) at the same place where my script (Save)
That's not what I'm asking though. You have to attach this script to a game object, right? Or else it won't run. It'll just sit there.
This script I attached to game object (Prefab) but it is not work
Your answer
Follow this Question
Related Questions
How to make camera rotation relative to target. 3 Answers
How do I disable mouse to turning right 0 Answers
Mathf.Clamp is 'Sticky'? 1 Answer
First Person Camera Rotation 360 degrees on all axis 0 Answers
Camera X and Y velocity HELP! 0 Answers