fbpx

Udemy Class Galaxy ...
 

Udemy Class Galaxy Shooter Issue  

   RSS

0

When i create the laser game object i get the error "unassigned reference exception". In the inspector i've assigned the laser object to the prefab. I found the following link online which describes the issue i'm seeing:

https://answers.unity.com/questions/1509777/unassigned-reference-exception-although-it-is-assi.html

 

The error happens when Instantiate occurs. 

Gerry

 

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

public class Player : MonoBehaviour
{
[SerializeField]
private float _speed = 3.5f;

private float _max_y_threshold = 0;
private float _min_y_threshold = -3.8f;
private float _x_pos_threshold = 11.3f;
private float _x_neg_threshold = -11.3f;

//[SerializeField]
public GameObject gameLaser;

// Start is called before the first frame update
void Start()
{
// take current position and give start position
transform.position = new Vector3(0, 0, 0);
}

// Update is called once per frame
void Update()
{
CalculateMovement();

if (Input.GetKeyDown(KeyCode.Space))
{
Instantiate(gameLaser, transform.position, Quaternion.identity);
}
}

void CalculateMovement()
{
float horizontalInput = Input.GetAxis("Horizontal");
transform.Translate(Vector3.right * _speed * horizontalInput * Time.deltaTime);

float verticalInput = Input.GetAxis("Vertical");
transform.Translate(Vector3.up * _speed * verticalInput * Time.deltaTime);

if (transform.position.x >= _x_pos_threshold)
{
transform.position = new Vector3(_x_neg_threshold, transform.position.y, 0);
}

else if (transform.position.x <= _x_neg_threshold)
{
transform.position = new Vector3(_x_pos_threshold, transform.position.y, 0);
}

transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, _min_y_threshold, _max_y_threshold), 0);
}
}

 

4 Answers
0

When you run the game, is your Game Laser assigned in the inspector? Make sure you're assigning the prefab, and not the laser that you may have in the scene view. 

Can you post a picture of your editor setup for this script. 

Mahalo,

Jonathan Weinberger
GameDevHQ

0

How do i do that? I have a screen shot but i can't attach a file, i'm prompted to enter a URL

 

Gerry

 

Can you send me an email address, it would be much easier.

Gerry

@gtrigdon

You should be able to drag the piccy into the answer Text Pane and drop it.

added: or use the Attach File -> Choose File button

 

Regards,

0

I can't. It may be because I have a free account here but i'm a paying student from Udemy Course.

I assume you talking about the toolbar option in the "Answer" Form?

Gerry

0

Never mind, I see it now at the bottom... I have attached the file of the Unity Screen. As you see it is attached to the prefab

@gtrigdon

your laser has the Player script attached....why? 

I think I got it figured out. I had to attach the prefab laser to the player in the scene. I'm a bit confused based on the course content but this seems to work now.

Gerry

Share:

 

 

Scroll to Top

Please Login or Register