Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 RVDL_IT · May 02, 2017 at 09:39 PM · c#errorcompiler errorcs0120cs0118

Error Code CS0118 or CS0120

I have a pick up items script and I get error code CS0120.

This is my PickUp script (the error code is at line 35):

 using UnityEngine;
 using System.Collections;
 using CameraRayCastLine;
 
 namespace CarryingItems {
 
     public class PickUp : MonoBehaviour {
     
         // The variables for the positions.
             // Forward drop offset.
                 public float OffsetDropPosition = 2;
                 
         // The Spherecast information.
             // The radius of the object SphereCast.
                 public float SphereCastRadius = 2.5f;
             // The layermask.
                 public LayerMask Player;
             
         // The boolean that checks if the camera's RayCast is hit.
             public bool CameraRayHit;
         
         // The Player and the tagged objects.
             public GameObject CarryableObject;
             public GameObject Character;
             public Transform CharacterTransform;
             
         // Is an item picked up at the moment?
             public bool ItemPickedUp;
         
         void Awake() {
             // The origin of the objects.
                 Character = GameObject.Find("Character");
                 CharacterTransform = GameObject.Find("Character").transform;
             // The code that references the RayCast script of the camera.
             //This is where I get the error. If I take away the .PickUpRayCast (the class) I get the CS0118 error.
                 CameraRayHit = Camera.GetComponent<CameraRayCastLine.PickUpRayCast>().CameraRayCast;
         }
 
         void Start() {
             // The code that tells you how to pick up an object through the debug log.
                 Debug.Log("Press E to pick up an object.");
         }
         
         void Update() {
             
         }
         
         void PickUpItemCode() {
             // A RayCast variable.
                 RaycastHit hit;
             // If another item isn't picked up at the moment.
                 if(ItemPickedUp == false) {
                     Debug.Log("No items picked up.");
                     // If you're in the SphereCast.
                         if(Physics.SphereCast(CarryableObject.transform.position, SphereCastRadius, transform.forward, out hit, 10, Player)) {
                             Debug.Log("Spherecast = true.");
                             // If you're looking at the item.
                                 if (CameraRayHit == true) {
                                     // A code that says you can pick up the item in question.
                                         Debug.Log("Press E to pick up the" + CarryableObject.name + ".");
                                     // If you press the E key.
                                         if (Input.GetKeyDown("e")) {
                                             // The position relative to the character.
                                                 CarryableObject.transform.position = new Vector3(Character.transform.position.x + 1, Character.transform.position.y + 1, Character.transform.position.z + 1);
                                             // The rotation relative to the character.
                                                 CarryableObject.transform.rotation = Character.transform.rotation;
                                             // The gravity.
                                                 CarryableObject.GetComponent<Rigidbody>().useGravity = false;
                                             // A message showing that the object was picked up.
                                                 Debug.Log("You picked up the " + CarryableObject.name + "! Good for you!");
                                                 Debug.Log("Press the left mouse button to use an object.");
                                                 Debug.Log("Press E to drop an object.");    
                                             // The code that acitvates the boolean that is needed for the dropping script.
                                                 ItemPickedUp = true;
                                             // The code that makes the chracter the new objects parent.
                                                 CarryableObject.transform.SetParent(CharacterTransform);
                                         }
                                 }
                         }
                 }
                 else {
                     // The code that makes sure that the pick up boolean is not active when no item is picked up.
                         ItemPickedUp = false;
                 }
         }
 
         void DropItemCode() {
             // If you're holding an item.
                 if(ItemPickedUp == true){
                     // If you press the E key.
                         if(Input.GetKeyDown("e")){
                             // The drop position.
                                 CarryableObject.transform.position = new Vector3(CarryableObject.transform.position.x, CarryableObject.transform.position.y, CarryableObject.transform.position.z + OffsetDropPosition);
                             // The gravity gets reenabled.
                                 CarryableObject.GetComponent<Rigidbody>().useGravity = true;
                         }
                 }
         }
     }
 }

And my RayCast script that I'm trying to reference:

 using UnityEngine;
 using System.Collections;
 
 namespace CameraRayCastLine {
 
     public class PickUpRayCast : MonoBehaviour {
         
         // The camera gameobject.
             public GameObject Camera;
             
         // The RayCast information.
             // The length of the RayCast. 
                 public float RayCastLength = 10;
             // The bool for the RayCast.
                 public static bool CameraRayCast;
             // The code that maked only the carryable items layer be seen.
                 public LayerMask CarryableObject;
 
         void Awake () {
             // The code that finds the camera.
                 Camera = GameObject.Find("MainCamera");
         }
     
         void Update () {
             // The actual RayCast.
                 bool CameraRayCast = Physics.Raycast(Camera.transform.position, Camera.transform.forward, RayCastLength,CarryableObject);
         }
     }
 }

Any help is appreciated.

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
Best Answer

Answer by bobisgod234 · May 03, 2017 at 12:38 AM

 CameraRayHit = Camera.GetComponent<CameraRayCastLine.PickUpRayCast>().CameraRayCast;

You have no variable called "Camera", so you are trying to call GetComponent like its a static function of Unity's Camera class, and its not a static function.

Since your "CameraRayCast" variable is a static, you can just replace the above line with

 CameraRayHit = CameraRayCastLine.PickUpRayCast.CameraRayCast;
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

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

Can't add script to gameobject even though there are no compiler errors in the script and the class name matches the script name 1 Answer

Why is this error coming up in my code ? 1 Answer

Distribute terrain in zones 3 Answers

CS0118 Error 1 Answer

Help with more code 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