- Home /
Change and Access another scripts Variables
I need to change and access another scripts variables in c# i have seen many answers to this question except none of them are in c# so I keep getting some sort of syntax error. Here is my code.
using UnityEngine;
using System.Collections;
public class Camera : MonoBehaviour
{
public GameObject Player;
public GameObject Camera1;
public GameObject Side1;
public GameObject Side2;
public GameObject Side3;
public GameObject Side4;
public float DistanceX;
public float DistanceY;
public float DistanceZ;
public float RotationX;
public float RotationY;
public float RotationZ;
public float Width = Screen.width/2;
public float Height = Screen.height/2;
public string Position = "First";
public bool EscClicked = false;
public var MovementScript = GetComponent(Movement);
//Movement MovementScript = GetComponent<Movement>();
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(EscClicked == false)
{
if(Input.GetKeyDown(KeyCode.Alpha2))
{
Position = "Second";
MovementScript.Mode = 1;
}
if(Input.GetKeyDown(KeyCode.Alpha3))
{
Position = "Third";
movementScript.Mode = 1;
}
if(Input.GetKeyDown(KeyCode.Alpha4))
{
Position = "Fourth";
movementScript.Mode = 1;
}
if(Input.GetKeyDown(KeyCode.Alpha1 ))
{
Position = "First";
movementScript.Mode = 1;
}
if(Position == "First")
{
DistanceX = Player.transform.position.x;
DistanceY = Player.transform.position.y;
DistanceZ = Player.transform.position.z - 10;
RotationX = 0;
RotationY = 0;
RotationZ = 0;
Camera1.transform.position = new Vector3(DistanceX, DistanceY, DistanceZ);
Camera1.transform.rotation = Quaternion.Euler(RotationX, RotationY, RotationZ);
Side1.GetComponent<MeshRenderer>().enabled = false;
Side2.GetComponent<MeshRenderer>().enabled = true;
Side3.GetComponent<MeshRenderer>().enabled = true;
Side4.GetComponent<MeshRenderer>().enabled = true;
}
if(Position == "Second")
{
DistanceX = Player.transform.position.x;
DistanceY = Player.transform.position.y;
DistanceZ = Player.transform.position.z + 10;
RotationX = 0;
RotationY = 180;
RotationZ = 0;
Camera1.transform.position = new Vector3(DistanceX, DistanceY, DistanceZ);
Camera1.transform.rotation = Quaternion.Euler(RotationX, RotationY, RotationZ);
Side1.GetComponent<MeshRenderer>().enabled = true;
Side2.GetComponent<MeshRenderer>().enabled = false;
Side3.GetComponent<MeshRenderer>().enabled = true;
Side4.GetComponent<MeshRenderer>().enabled = true;
}
if(Position == "Third")
{
DistanceX = Player.transform.position.x + 10;
DistanceY = Player.transform.position.y;
DistanceZ = Player.transform.position.z;
RotationX = 0;
RotationY = -90;
RotationZ = 0;
Camera1.transform.position = new Vector3(DistanceX, DistanceY, DistanceZ);
Camera1.transform.rotation = Quaternion.Euler(RotationX, RotationY, RotationZ);
Side1.GetComponent<MeshRenderer>().enabled = true;
Side2.GetComponent<MeshRenderer>().enabled = true;
Side3.GetComponent<MeshRenderer>().enabled = false;
Side4.GetComponent<MeshRenderer>().enabled = true;
Debug.Log(Camera1.transform.rotation);
}
if(Position == "Fourth")
{
DistanceX = Player.transform.position.x - 10;
DistanceY = Player.transform.position.y;
DistanceZ = Player.transform.position.z;
RotationX = 0;
RotationY = 90;
RotationZ = 0;
Camera1.transform.position = new Vector3(DistanceX, DistanceY, DistanceZ);
Camera1.transform.rotation = Quaternion.Euler(RotationX, RotationY, RotationZ);
Side1.GetComponent<MeshRenderer>().enabled = true;
Side2.GetComponent<MeshRenderer>().enabled = true;
Side3.GetComponent<MeshRenderer>().enabled = true;
Side4.GetComponent<MeshRenderer>().enabled = false;
}
if(Input.GetKeyDown(KeyCode.Escape))
{
EscClicked = true;
Player.GetComponent<CharacterController>().enabled = false;
Player.GetComponent<Rigidbody>().isKinematic = true;
}
}
}
void OnGUI()
{
if(EscClicked == true)
{
Player.GetComponent<Rigidbody>().isKinematic = false;
if(GUI.Button(new Rect(Width - 50, Height, 100, 30), "Main Menu"))
{
Application.LoadLevel(0);
}
if(GUI.Button(new Rect(Width - 50, Height + 35, 100, 30), "Help"))
{
Application.LoadLevel(2);
}
if(GUI.Button(new Rect(Width - 50, Height + 70, 100, 30), "Exit Pause"))
{
EscClicked = false;
Player.GetComponent<CharacterController>().enabled = true;
//Player.active = false; Does not re-activate
Player.GetComponent<Rigidbody>().isKinematic = false;
}
}
}
}
EDIT: I have No idea how to set it up in c# so I don't know what the syntax error is with this code the error is: The contextual keyword var may only appear within a local variable deceleration
Comment
Answer by Lovrenc · Jan 20, 2013 at 02:47 AM
There is plenty of examples even in c#. This is exactly the same question:
EDIT: Also if you want us to fix your errors, you will have to give us error message and a line on which it happens. "Some sort of syntax error" is really not telling much.