• 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 /
avatar image
0
Question by wyler0 · Jan 20, 2013 at 02:46 AM · c#variableaccessother

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

Answer by Lovrenc · Jan 20, 2013 at 02:47 AM

There is plenty of examples even in c#. This is exactly the same question:

Same question, c#

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.

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 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

Related Questions

Can't get script to access other script/class (c#) 1 Answer

How to access gameObject variable script 2 Answers

Using a script as a member variable in another script. 2 Answers

Editing a variable from another script on collision 3 Answers

Access variables from another script 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges