• 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 Beaullo · Jan 21, 2014 at 07:07 PM · error

.facingRight inaccessable due to protection level

I just started playing with Unity like 3 days ago. I've watched both the tutorials on the site and created my own player control script. I don't know how to do guns or projectiles or even melee so I tried to copy and paste the gun script from the demo provided. I tried to tweak it to reference my script but I keep getting t$$anonymous$$s

Assets/obama/scripts/Gun.cs(32,39): error CS0122: `obamacontroller.facingRight' is inaccessible due to its protection level

I also don't understand the (x,x). I realize that the 32 is the line but what's the second digit mean?

I've also added an older screenshot of my 'game'..

Here are my two scripts.
T$$anonymous$$s is the player control.

 using UnityEngine;
 using System.Collections;
 
 public class obamacontroller : MonoBehaviour
 {
     public float maxSpeed = 10f;
     bool facingRight = true;
 
     Animator anim;
     
 
     bool grounded = false;
     public Transform groundCheck;
     float groundRadius = 0.2f;
     public LayerMask whatIsGround;
     public float jumpForce = 700f;
 
     bool doubleJump = false;
 
 
 
     void Start () 
     {
         anim = GetComponent<Animator> ();
     }
 
     void FixedUpdate () 
     {
         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
         anim.SetBool ("Ground", grounded);
 
         if (grounded)
                         doubleJump = false;
 
         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
 
 
 
 
         float move = Input.GetAxis ("Horizontal");
 
         anim.SetFloat ("Speed", Mathf.Abs(move));
 
         rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
 
         if(move > 0 &&!facingRight)
                         Flip ();
                 else if(move < 0 && facingRight)
                         Flip ();
     }
 
     void Update()
     {
         
                 if (Input.GetKeyDown ("h")) {
                         punch = true;
                 }
             
                         if ((grounded || !doubleJump) && Input.GetKeyDown (KeyCode.Space)) {
                                 anim.SetBool ("Ground", false);
                                 rigidbody2D.AddForce (new Vector2 (0, jumpForce));
 
                                 if (!doubleJump && !grounded)
                                         doubleJump = true;
                         }
                 }
 
     void Flip()
     {
 
     
 
 
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 
     }

and then the gun.cs

using UnityEngine; using System.Collections;

public class Gun : MonoBehaviour { public Rigidbody2D rocket; // Prefab of the rocket. public float speed = 20f; // The speed the rocket will fire at.

 public obamacontroller playerCtrl;        // Reference to the PlayerControl script.
 public Animator anim;                    // Reference to the Animator component.


 void Awake()
 {
     // Setting up the references.
     anim = transform.root.gameObject.GetComponent<Animator>();
     playerCtrl = transform.root.GetComponent<obamacontroller>();
 }


 public void Update ()
 {
     // If the fire button is pressed...
     if(Input.GetButtonDown("Fire1"))
     {
         // ... set the animator Shoot trigger parameter and play the audioclip.
         anim.SetTrigger("Shoot");
         audio.Play();

         // If the player is facing right...
         if(playerCtrl.facingRight)
         {
             // ... instantiate the rocket facing right and set it's velocity to the right. 
             Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
             bulletInstance.velocity = new Vector2(speed, 0);
         }
         else
         {
             // Otherwise instantiate the rocket facing left and set it's velocity to the left.
             Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,180f))) as Rigidbody2D;
             bulletInstance.velocity = new Vector2(-speed, 0);
         }
     }
 }

}

alt text

obama.jpg (369.6 kB)
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 Jamora · Jan 23, 2014 at 03:36 PM

If you don't provide an access modifier to variables they will have their default access. In C# it is private, in UnityScript it is public.

facingRight is assigned its default modifier -- private --, thus is inaccessible outside that class.

If you don't know what access modifiers do, have a look at t$$anonymous$$s MSDN page w$$anonymous$$ch explains what they do.

The 2nd digit in all the error codes is the character at w$$anonymous$$ch the compiler encountered the error.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

How to change variables on a new gameobject's script 1 Answer

Unfixable Assets/Scripts/PlayerAttack.cs(30,15): error CS1525: Unexpected symbol `private' 1 Answer

need help, with greater than statment 1 Answer

Unity error "scripts exist in multiple locations" 1 Answer

dont work maxdistance 0 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