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 Willga321 · Feb 28, 2014 at 02:16 AM · objectreferencenon-staticcontext

Get variable from seperate script.

Hi, I know this question is quite common but I'm a total newbie to C# I would like the variable isShowingMouse to be able to be changed from a second script so that I can disable the mouse when ever an object is clicked on. My mouse code works, I just can't get the other script to interact with it. I get the error "An object reference is required to access non-static member MouselockC.isShowingMouse'" and the error "The name isShowingMouse' does not exist in the current context". Heres the code I have so far.

This is my mouse script that works

 using UnityEngine;
 using System.Collections;
 
 public class MouselockC : MonoBehaviour {
 
     public bool isShowingMouse = true;
     
     void Start(){ 
 
         ToggleMouse(false); 
     }
     
     void Update()
     { 
         if(Input.GetMouseButtonDown(0))
         { Fire(); }
         if(Input.GetKeyDown(KeyCode.E))
         { ToggleMouse(!this.isShowingMouse); }
     }
     
     public void ToggleMouse(bool showMouse)
     {
         Screen.lockCursor = !showMouse;
         Screen.showCursor = showMouse;
         
         this.isShowingMouse = !this.isShowingMouse;
     }
     
     private void Fire()
     {
         Transform cameraTransform = Camera.main.transform;
         Ray fireRay = new Ray(cameraTransform.position, cameraTransform.forward);
         
         RaycastHit hit;
         if(!Physics.Raycast(fireRay, out hit))
         { return; }
         
         Debug.Log("Hit: " + hit.collider.name);
     }
 
 
 }

And here is the script I am trying to use control the above script.

 using UnityEngine;
 using System.Collections;
 
 public class Free_Mouse : MonoBehaviour {
     
 
     // Use this for initialization
     void Start () {
         GameObject teleport = GameObject.FindGameObjectWithTag("teleport");
         teleport.GetComponent<MouselockC>();
         
     }
     
     // Update is called once per frame
     void Update () {
 
         isShowingMouse = MouselockC.isShowingMouse;
 
         //hasForce = script.isShowingMouse(); 
 
         if(MouselockC.isShowingMouse == false)
         { 
             Debug.Log("Pressed left click.");
         }
     }
 
 
     void OnMouseDown(){
         Destroy (this.gameObject);
         MouselockC.isShowingMouse = false;
     }
 }

Thanks so much for any help!

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 robertbu · Feb 28, 2014 at 02:29 AM

You are really close. Change line 10 in the second script to:

 MouselockC mouselockC = teleport.GetComponent<MouselockC>();

Then change line 21 in the same script to:

  if(mouselockC.isShowingMouse == false)


MouselockC is the class or script. A script can be attachd to multiple objects. What you need is access to the specific instance of that script attached to a specific object. (called a component in Unity speak). You are getting the object, but you are not saving a reference to the specific component.

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

20 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

Related Questions

An object reference is required to access non-static member 1 Answer

Noob Question, Object reference 1 Answer

Object reference not set to an instance of an object 1 Answer

NullReferenceException: Object reference not set to an instance of an object 1 Answer

Object Refererence not set yada yada... 2 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