Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
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.

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

Last time I'll ask for help on this lol 2 Answers

Collision object and access to a script variable? 1 Answer

Change a Variable with another script not working (C#) 4 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