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 oliver-jones · Feb 21, 2011 at 11:27 PM · booleanstaticpublic

Help With C# Static Boolean!

Hello, I'm a JavaScript user, and I need to change a C# script (its the MouseLook Unity have).

What I need to do is it access a boolean from another script, but it keeps saying '..required to access non static ..'

I need to access the headLock boolean (to make it static), how would I go about doing that:

using UnityEngine; using System.Collections;

[AddComponentMenu("Camera-Control/Mouse Look")] public class MouseLook : MonoBehaviour {

 public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
 public RotationAxes axes = RotationAxes.MouseXAndY;
 public float sensitivityX = 15F;
 public float sensitivityY = 15F;

 public float minimumX = -360F;
 public float maximumX = 360F;

 public float minimumY = -60F;
 public float maximumY = 60F;

 float rotationY = 0F;
 public bool headLock = false;

 void Update ()
 {
     if (axes == RotationAxes.MouseXAndY && headLock == false)
     {
         float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

         rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
         rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

         transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
     }
     else if (axes == RotationAxes.MouseX && headLock == false)
     {
         transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
     }
     else if (headLock == false)
     {
         rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
         rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

         transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
     }

     if(Input.GetButton("Jump")){
         headLock = true;
         print("headLock = true");
     }
     else{
         headLock = false;
         print("headLock = false");
     }


 }

 void Start ()
 {
     // Make the rigid body not change rotation
     if (rigidbody)
         rigidbody.freezeRotation = true;
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Bampf · Feb 21, 2011 at 11:49 PM

Add the static keyword to the declaration:

public static bool headLock = false;

This allows other scripts to reference the value as follows:

MouseLook.headLock

without having an actual object of type MouseLook. (And no matter how many MouseLook objects exist in your scene, they will all share the same headLock value.)

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

Answer by Peter G · Feb 21, 2011 at 11:50 PM

A few points:

  1. You left out the most important word in that error "an instance is required...". You need to have the script attached to an object then use GetComponent<>() to reference it.

  2. Your idea of static doesn't seem to be correct. A static variable is allocated statically. That means it is created when the assembly is loaded. You access it by ClassName.Variable which lets you access it from other classes, but it isn't designed to be treated like an instance variable. More than likely, if you need to access a variable attached to a script on a GameObject then you want to use GetComponent<>(). Static variables are for something like Time.deltaTime (ok, it's technically a property) where it isn't specific to any specific instance.

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

No one has followed this question yet.

Related Questions

public static bool not working at all. 0 Answers

Probably Dumb Question: Update(), Start(), etc. Don't Work Inside Classes (JavaScript)...? 2 Answers

How do I access a static function in JS? 1 Answer

Script not responding to public variable change 1 Answer

Public boolean can't be accessed due to its protection level. 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