Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by kaikhan · Jun 18, 2016 at 05:44 AM · c#raycast

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

 }

   

}

Comment
Add comment · Show 2
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 mwnDK1402 · Jun 18, 2016 at 03:22 PM 0
Share

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.

avatar image kaikhan · Jun 19, 2016 at 06:28 PM 0
Share

Yah i have the player in its correct field.

1 Reply

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

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


}

Comment
Add comment · Show 3 · 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 kaikhan · Jun 19, 2016 at 06:32 PM 0
Share

Putting the statement in the same brackets as the ray cast doesn't change anything, is this just for keeping it cleaner?

avatar image RayCastX kaikhan · Jun 20, 2016 at 03:13 PM 0
Share

Sometimes, raycast hit info returns null.

avatar image kaikhan · Jun 20, 2016 at 10:18 PM 0
Share

Ok gotcha. Thanks for that info

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

wall check is always returning true even though the distance doesn't fit my parameters :( 1 Answer

Plane is blocked all Raycast 0 Answers

I can't get Physic.Raycast to work,My Physics.Raycast don't work 0 Answers

Inconsistent Jump Heights 1 Answer

Make a Raycast pass through a layer? 0 Answers


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