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;
}
}
}