Question by
Bleakmountain50 · Sep 17, 2017 at 02:51 AM ·
scripting problemerrorconsole errors
only using GetMouseButtonDown(0) once
here's my code. the console says the issue is line 19 and 48
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickUp : MonoBehaviour {
public float speed = 10;
public bool canHold = true;
public GameObject cube;
public Transform guide;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
print ("clicked");
if (!canHold)
throw_drop();
else
Pickup();
}
if (!canHold && cube)
cube.transform.position = guide.position;
}//update
//We can use trigger or Collision
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Pick")
if (!cube) // if we don't have anything holding
cube = col.gameObject;
}
//We can use trigger or Collision
void OnTriggerExit(Collider col)
{
if (col.gameObject.tag == "Pick")
{
if (canHold)
cube = null;
}
}
private void Pickup()
{
cube.GetComponent<Rigidbody>().isKinematic = true;
if (!cube)
return;
//We set the object parent to our guide empty object.
cube.transform.SetParent(guide);
//Set gravity to false while holding it
cube.GetComponent<Rigidbody>().useGravity = false;
//we apply the same rotation our main object (Camera) has.
cube.transform.localRotation = transform.rotation;
//We re-position the cube on our guide object
cube.transform.position = guide.position;
canHold = false;
}
private void throw_drop()
{
cube.GetComponent<Rigidbody>().isKinematic = false;
if (!cube)
return;
//Set our Gravity to true again.
cube.GetComponent<Rigidbody>().useGravity = true;
// we don't have anything to do with our cube field anymore
cube = null;
//Apply velocity on throwing
guide.GetChild(0).gameObject.GetComponent<Rigidbody>().velocity = transform.forward * speed;
//Unparent our cube
guide.GetChild(0).parent = null;
canHold = true;
}
}//class
Comment