- Home /
C# Gravity Gun. Error CS0120
So I managed to get hold of a script in javaScript for making an gravity gun similar to that in half life however the game I'm working on is purely C# so I tried my hand at converting the code over to C#. I've managed to get most of the code error free but I'm still picking 1 error with no idea as to fix it. I know it has something to do with the GameObject.tag which I'm assuming is in those beginning if statements but have no idea I could fix it.
The error in question that I'm getting is Error CS0120 in which it states the following:
" Assets/Scripts/PickUp.cs(22,30): error CS0120: An object reference is required to access non-static member `UnityEngine.GameObject.tag' "
The code for it below. I have also included another script which works in conjunction with this (Again I converted the code from javaScript to C#).
PickUp Script
using UnityEngine;
using System.Collections;
public class PickUp : MonoBehaviour {
public Quaternion objectRot;
public GameObject pickObj;
public bool canpick = true;
public bool picking = false;
public bool guipick = false;
public Vector3 objectPos;
public GameObject pickref;
public GameObject mainCamera;
// Use this for initialization
void Start () {
mainCamera = GameObject.FindWithTag("MainCamera");
pickref = GameObject.tag("pickupref");
pickObj = pickref;
}
// Update is called once per frame
void Update ()
{
Ray ray = mainCamera.camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 10) && hit.collider.gameObject.tag == "pickup")
{
guipick = true;
}
if (Physics.Raycast(ray, out hit) && hit.collider.gameObject.tag != "pickup")
{
guipick = false;
}
objectPos = transform.position;
objectRot = transform.rotation;
if(Input.GetMouseButtonDown(0) && canpick)
{
picking = true;
Ray rayTwo = mainCamera.camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hitTwo;
if (Physics.Raycast( rayTwo, out hitTwo, 10) && hitTwo.collider.gameObject.tag == "pickup")
{
pickObj = hitTwo.collider.gameObject;
hitTwo.rigidbody.useGravity = false;
hitTwo.rigidbody.isKinematic = true;
hitTwo.collider.isTrigger = true;
hitTwo.transform.parent = gameObject.transform;
hitTwo.transform.position = objectPos;
hitTwo.transform.rotation = objectRot;
}
}
if (Input.GetMouseButtonUp(0) && picking)
{
picking = false;
canpick = false;
}
if (Input.GetMouseButtonDown(0) && !canpick && pickObj.GetComponent<PickedUpObject>().refuseThrow != true)
{
canpick = true;
pickObj.rigidbody.useGravity = true;
pickObj.rigidbody.isKinematic = false;
pickObj.transform.parent = null;
pickObj.collider.isTrigger = false;
pickObj.rigidbody.AddForce(transform.forward * 8000);
pickObj = pickref;
}
if (Input.GetMouseButtonDown(1) && !canpick && pickObj.GetComponent<PickedUpObject>().refuseThrow != true)
{
canpick = true;
pickObj.rigidbody.useGravity = true;
pickObj.rigidbody.isKinematic = false;
pickObj.transform.parent = null;
pickObj.collider.isTrigger = false;
pickObj = pickref;
}
}
PickedUpObject Code
public class PickedUpObject : MonoBehaviour {
public bool refuseThrow = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag != "player" && other.GameObject.tag != "pickto")
{
refuseThrow = true;
}
}
void OneTriggerExit(Collider other)
{
if (other.gameObject.tag != "player" && other.GameObject.tag != "pickto")
{
refuseThrow = false;
}
}
}
Okay so I've managed to find the line in particular that is causing this error being line 55 in the PickUp code however I have no idea how to fix it. Anyone have an idea?
Answer by siaran · Mar 13, 2015 at 02:27 PM
pickref = GameObject.tag("pickupref");
That is the line which causes the error you posted. Maybe you meant to use GameObject.FindWithTag?
also, I feel like I write this every time, but don't use GameObject.Find or its derivatives.
Answer by Dragonfly3r · Mar 20, 2015 at 05:50 AM
Aye that seemed to fix it cheers Siaran.
I was wondering if you might be able to help me with 2 other problems that seem to occur when using the code.
Firstly if the pickref & pickto gameobjects when attached to the player's camera if the player should walk into a wall and these gameObjects go through said wall or other object then the script ceases to work as in can't pick up object. If a object such as a cube is already being carried and the player collides into an object such as a wall or looks down at the floor. The cube will remain attached but slide on through the object and then cease to response to commands e.g. drop cube/ launch cube.
The other problem I'm facing is when the player tries to pick up something that is not got the tag "pickup" e.g. nothing/ non-pickable object and it seems to refer to the last if statement at line 71. I made a temporary fix in the code by doing this
if (Input.GetMouseButtonUp(0) && picking)
{
picking = false;
canpick = false;
}
else if (Input.GetMouseButtonUp(0) && !picking)
{
canpick = true;
}
But this makes little different apart from allow the script to again respond. As currently at the moment if the player picks up nothing it will make both canpick and picking = false. however if nothing is picked up by clicking the mouse button again then you make canpick true so you can begin to pick objects back up. However this isn't what I want but instead it to just not allow the player to pick up anything unless it has the proper tag.