- Home /
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
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;