Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Dragonfly3r · Mar 13, 2015 at 12:37 PM · c#javascriptgravityguncs0120

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




Comment
Add comment · Show 1
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
avatar image Dragonfly3r · Mar 13, 2015 at 12:51 AM 0
Share

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?

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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?

Comment
Add comment · Show 1 · 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
avatar image siaran · Mar 13, 2015 at 02:29 PM 0
Share

also, I feel like I write this every time, but don't use GameObject.Find or its derivatives.

avatar image
0

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.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Allow picked up object to collide with game level GameObjects 1 Answer

separate script to reload. 0 Answers

Trying to figure out how to translate these gravity script to Javascript 2 Answers

Multiple Cars not working 1 Answer


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