- Home /
Question by
RadZaeem · May 18, 2014 at 11:41 AM ·
errorgameobjectraycastobject
Cannot pass GameObject found by raycast
I use raycast, found the object (debug.log shows it exist) however when i try to pass it to another object, got NullReferenceError Object reference not set to an instance of an Object C#
private void CheckPick() {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (iInput.touchCount > 0 && iInput.GetTouch (0).phase == TouchPhase.Began) {
if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
if (hit.collider.gameObject.tag == "EnemyMissile"){
Debug.DrawLine (ray.origin, hit.point);
GameObject em = hit.collider.gameObject;
PlayerMissile m;
m = Instantiate(missilePrefab, em.transform.position, Quaternion.identity) as PlayerMissile;
Debug.Log ("missile is " + em); // this shows it found GameObject. works fine.
m.enemyMissile = em; // got NullReferenceError here. error when use this code
enemyMissile is public variable, type GameObject,in another script
Comment
What is the type of 'missilePrefab'? If it is not Player$$anonymous$$issile, then that is your problem.