Raycast info and Error
Can anyone help me figure out why i'm getting this error from my script, it seem to be working perfectly fine but i cant stand the error message.
NullReferenceException: Object reference not set to an instance of an object YoYo.DrawToObject () (at Assets/YoYo.cs:71) YoYo.Update () (at Assets/YoYo.cs: It's actually on line 81!!
I'm relatively new to coding and would also like to know if there is a more efficient or cleaner way to achieve the same results from this script. Also i am trying to get the player or object to keep moving to the desired point when the ray cast is no longer looking at the object that is tagged. Ideas Suggestions? Here is my script
using UnityEngine; using System.Collections;
public class YoYo : MonoBehaviour { public GameObject yoyo; public GameObject player; public Transform startPosition; private bool mouseClick = false; private bool mouseReturnClick = false; public float speed = 1.0f; public float dragSpeed = 3.0f; public float returnSpeed = 10.0f; public float timer = 0.0f; string ColliderHitByRaycast;
public GameObject playerCamera;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
OnMouseDown();
GrappleReturn();
DrawToObject();
PullObjectTo();
}
// The Bool as true or false for the grapple.
void OnMouseDown()
{
if(Input.GetMouseButtonDown(0))
{
mouseClick = true;
}
if (Input.GetMouseButtonDown(1))
{
mouseReturnClick = true;
mouseClick = false;
}
}
// Pulls the player to object
void DrawToObject()
{
RaycastHit hit;
Ray ray = new Ray(playerCamera.transform.position, playerCamera.transform.forward);
if (Physics.Raycast(ray, out hit))
{
ColliderHitByRaycast = hit.collider.tag;
}
if (ColliderHitByRaycast == "GrappleDrag" && mouseClick == true)
{
yoyo.transform.position = Vector3.MoveTowards(yoyo.transform.position, hit.transform.position, speed * Time.deltaTime);
}
if (yoyo.transform.position == hit.transform.position)
{
player.transform.position = Vector3.MoveTowards(player.transform.position, hit.transform.position, dragSpeed * Time.deltaTime);
}
}
void PullObjectTo()
{
RaycastHit hit;
Ray ray = new Ray(playerCamera.transform.position, playerCamera.transform.forward);
if (Physics.Raycast(ray, out hit))
{
ColliderHitByRaycast = hit.collider.tag;
}
if (ColliderHitByRaycast == "GrapplePull" && mouseClick == true)
{
// moves the object directly to the box labled grapple
yoyo.transform.position = Vector3.MoveTowards(yoyo.transform.position, hit.transform.position, speed * Time.deltaTime);
}
if (yoyo.transform.position == hit.transform.position)
{
hit.transform.position = Vector3.MoveTowards(hit.transform.position, startPosition.transform.position, dragSpeed * Time.deltaTime);
}
}
void GrappleReturn()
{
if (mouseReturnClick == true)
{
transform.Rotate(Vector3.right, -180.0f);
yoyo.transform.position = Vector3.MoveTowards(yoyo.transform.position, startPosition.position, returnSpeed * Time.deltaTime);
}
if (yoyo.transform.position == startPosition.transform.position)
{
mouseReturnClick = false;
}
}
}
Have you assigned a value to the field "player"? It's public, so you should be able to drag the desired GameObject into the field in the editor.
Answer by RayCastX · Jun 18, 2016 at 11:15 PM
put this is inside
if (Physics.Raycast(ray, out hit)) {
ColliderHitByRaycast = hit.collider.tag;
if (yoyo.transform.position == hit.transform.position) {
hit.transform.position = Vector3.MoveTowards(hit.transform.position, startPosition.transform.position, dragSpeed * Time.deltaTime);
}
}
Putting the statement in the same brackets as the ray cast doesn't change anything, is this just for keeping it cleaner?