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 /
avatar image
0
Question by ToxicC00kie1001 · Feb 25, 2019 at 02:04 AM · raycastraycastingraycasthitrayraycasts

Raycast returns null for no apparent reason

Ok, so quick question, I made a simple:

 Ray ray;

Then in the update() I made a simple:

 ray = Camera.current.ScreenPointToRay(Input.mousePosition);
 Debug.Log(Camera.current.ScreenPointToRay(Input.mousePosition));

And for some reason, in the console, the debug.log registers a ray being casted while ray just thinks it's null.

Any ideas?

This is the debug.log output: https://i.stack.imgur.com/OpXFG.png

This is the ray output: https://i.stack.imgur.com/pmlex.png

In case I overlooked something, here's the full script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class TankController : MonoBehaviour
 {

 Vector3 targetPosition;
 Vector3 lookAtTarget;

 public Vector4 selectedColor;
 public Vector4 deselectedColor;

 Quaternion playerRot;

 public float rotSpeed = 2;
 public float speed = 3;

 bool OneExecution = false;
 int count = 0;

 public bool Moving = false;
 public bool Selected = false;
 public bool UiSelected = false;
 public bool Hovering = false;

 public GameObject UI;
 public BoxCollider HitBox;

 public DoEverything Properties;
 public GameObject childObj;

 public MeshRenderer mrHighlight;

 public PlayerMaster playerMaster;
 int playerMasterTeam;

 SkinnedMeshRenderer[] skinnedMeshRenderersScan;
 public List<SkinnedMeshRenderer> skinnedMeshRenderersList = new List<SkinnedMeshRenderer>();

 Ray ray;
 RaycastHit hit;

 void Start()
 {
 Properties = GetComponentInChildren<DoEverything>(); //Get the DoEverything script

 childObj = Properties.InstancedEntity; //Get the object it will spawn

 if (mrHighlight.enabled != false && mrHighlight != null) //Make sure the highlight isn't enabled and not null
 {
     mrHighlight.enabled = false;
 }

 skinnedMeshRenderersScan = childObj.GetComponentsInChildren<SkinnedMeshRenderer>(); //Looks for all skinned mesh renderers in child object

 foreach (SkinnedMeshRenderer element in skinnedMeshRenderersScan) //For every object it finds
 {
     if (!skinnedMeshRenderersList.Contains(element)) //If it isn't already in this list
     {
         skinnedMeshRenderersList.Add(element); //Add to the list
     }
 }

 playerMasterTeam = playerMaster.Team;
 }

 void LClickRay()
 {

 }

 void RClickRay()
 {

 }

 void OnMouseEnter()
 {
 Hovering = true;
 foreach (SkinnedMeshRenderer element in skinnedMeshRenderersScan) //For every object it finds
 {
     element.material.color = selectedColor;
 }
 }

 void OnMouseExit()
 {
 Hovering = false;
 foreach (SkinnedMeshRenderer element in skinnedMeshRenderersScan) //For every object it finds
 {
     element.material.color = deselectedColor;
 }
 }

 void OnCollisionEnter(Collision collision)
 {
 if (collision.gameObject.tag == "Player")
 {
     Physics.IgnoreCollision(collision.collider, HitBox);
 }
 }

 void Move()
 {
 transform.rotation = Quaternion.Slerp(transform.rotation,
                                             playerRot,
                                             rotSpeed * Time.deltaTime);
 transform.position = Vector3.MoveTowards(transform.position,
                                         targetPosition,
                                         speed * Time.deltaTime);

 if (transform.position == targetPosition)
 {
     Moving = false;
 }
 }

 void Update()
 {
 if (Input.GetMouseButtonDown(0))
 {
     LClickRay();
 }
 if (Input.GetMouseButtonDown(1))
 {
     RClickRay();
 }

 if (Moving == true)
 {
     Move();
 }

 ray = Camera.current.ScreenPointToRay(Input.mousePosition);
 Debug.Log(Camera.current.ScreenPointToRay(Input.mousePosition));
 }
 }
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

145 People are following this question.

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

Related Questions

Can you figure out raycast origin position from RacyastHit? 2 Answers

My raycast is ignoring my tilemap collider 0 Answers

Raycast detects box collider, but not capsule collider 0 Answers

RayCast2d Not colliding with objects in other layers 1 Answer

Play Animation on Raycast *open and close some objects* 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