Kodeco Forums

Introduction to Unity: Getting Started – Part 2/2

In this second part of the Introduction to Unity tutorial you'll learn: "How to use the Project window to import and organize your assets", "The difference between GameObjects and Prefabs", "How physics work in Unity", "How cameras work and which types of projections are available" and "The basics of Materials in Unity".


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/770-introduction-to-unity-getting-started-part-2-2

I did the 2016 version of the tutorial and had a problem. Then I saw this updated version and tried it, but I’m having the same problems.

When the game starts, the asteroids don’t always generate on the same plane, so eventually I get 5 asteroids floating around, and they can’t be destroyed. This doesn’t happen to all of them, however, some are able to be shot.

I am getting this error, which I think is related: “UnassignedReferenceException: The variable screenDebris of GameManager has not been assigned.
You probably need to assign the screenDebris variable of the GameManager script in the inspector.
GameManager.Update () (at Assets/Scripts/GameManager.cs:31)”

Also, when I build the game and run it, there is no way to escape from the game back to the computer. I have to re-start the computer.

Currently I am doing this on a MacBook with OS 10.12.3 installed, and Unity 5.5.1f1 personal (64Bit).

Thanks,
Haole

Hi Haole,

Thank you for taking the time to have another crack at the updated tutorial! Let’s see if we can help get to the bottom of what’s going wrong.

  1. Asteroids spawning on the wrong plane occasionally:
    Troubleshooting is sometimes best done in Unity’s Scene View.
    Open your project in Unity
    In the Hierarchy, double click on “Spaceship” to zoom into the spaceship in the scene view.
    In the Scene view click the red “X axis” on the control at the top right of the window.
    You should be looking at you spaceship from the side now.
    Zoom out a little using the scroll wheel (or hold down Ctrl and Alt, right-click and move the mouse down to zoom out) and click play to preview your game…
    Ignore the Game view and watch the Scene view. Are the asteroids spawning on the same plane as your spaceship? Are they remaining on the same plane, or are they drifting off course as the game continues? If not double check the transforms of these elements against the positions described in the tutorial. Please let me know what the results are and I can help further.

  2. Your UnassignedReferenceException:
    This variable screenDebris is a public variable that should appear as an assignable field in the Inspector when you select your GameManager. The error suggests you have not dragged the “Debris” Game Object from the Hierarchy onto this assignable field. However, as you are spawning asteroids during gameplay I assume you have indeed completed this step! Is your console set to “Clear on Play”?
    Open the console window (either go to “Window” → “Console” or Command + Shift + C on the mac) And check that “Clear on Play” is selected to ensure the old logs are removed each time you enter play mode.

  3. Restating the computer to quit:
    As this tutorial is simply a “learning the basics of Unity’s user interface” We don’t get into creating user interface elements such as buttons and the like. If you create a standalone mac build and run the game it will keep running till you quit the application. Usually you should be able to move your cursor to the top of the screen to reveal the menu bar and navigate to “SpaceDebris” → “Quit” Alternaticaly, use the hot keys Command + q. However, depending on the settings used to build your application (“File” → “Build Settings” → “Player Settings”) you may not be able to access the menu bar, however, using the hot keys Command + q should still shut the application down regardless.

Please let me know how you get on with these suggestions… if you have any other difficulties, please do no hesitate to reach out.

Mark :]

Mark,

Thanks for the quick response. I went over what you suggested and here is what I came up with.

I’m not sure why it is different, but when I select the objects the in the prefab folder, the Ship is Z: 10, the Asteroid is Z: 12.5976, and the bullet (which I noticed is also on the wrong plane) is Z: 10.423. I ran the game, and the ship Z is still 10, but the asteroids are all at Z: 0, although visually, they are on different planes (although only slightly, and the rotation of some is Z: -180) and the bullet Z is 10 (Same as the ship, but visually it is off). The Sphere Collider component is in the Asteroid object, and the Z axis all says, 0. Yet this doesn’t explain why some asteroids get shot and some don’t (also, the ship runs into some asteroids and some it passes through). So I’ve done something wrong somewhere, but I’m not sure where. I’ve included a screenshot of looking down on the scene to show visually where everything is.

Looking at the sphere colliders also, the ship’s is set to Z: -0.72, the bullet is set to Z: 0, and the asteroid is Z: 0.89. I’ve verified the required inputs on the asteroid are the same, although the Z axis in Transform → Position is different. So I think there is an error there as well.

I’m rehashing the steps I think are were I made a mistake, but I’m still getting the same results. Maybe need to start from scratch.

  1. I verified on the console, “clear on play” is checked, but there were 999+ of that errors, so I cleared, it, ran the game again, and had 999+ of the error within a few seconds.

I’m finding it difficult working on this on my little laptop, I’m going to try to export the project to my iMac, where I have more space to work, and verify everything again. Or I will build the game again tonight (japan time) and see if I can find the error.

Thanks again, I’ll keep working it.

Haole

Hi Again,
Thank you for the information.
Two nuggets of info to help us understand the values you are seeing in your game:

  1. 3D Models can have “pivot points” that are not in the centre of the object… The spaceship is a prime example of that… rotate it around its Y axis and you’ll see it pivots around a point some distance underneath it. This is why the colliders for the objects are not at 0, 0, 0. We adjust the offset of the collider to make sure it encapsulates the bulk of the ship.
  2. Let’s say we had three game objects on the screen, called Game Object 1, Game Object 2 and Game Object 3.
    Now let us say that Game Object 1 is the child (in the hierarchy) of Game Object 2.
    It’s important to note that you now can not compare the transform position of Game Object 1 and Game Object 3.
    This is because Game Object 1’s transform is now in relation to it’s parent, Game Object 2.
    It is for this reason that objects can have different Z values but still be on the same plane within your game and conversely they may be in completely different positions on the screen even if their transforms are identical!

Keeping these things in mind… there are two things I think you should check to help you troubleshoot your game.

  1. The Debris Game Object may be positioned incorrectly.
    We need to ensure the asteroids spawn on the same plane as the spaceship. We do this with the positioning of the Debris Game Object in a two step process.
    We parent Debris with a part of the spaceship Game Object (missle_R_0) and set its transform to 0, 0, 0.
    And we then unparent Debris.
    The Debris Game Object should now exist on the same plane as the spaceship but not parented to it. Now each asteroid that we spawn is a child of Debris, and that is why we set their z position to 0 in code… so they will be at the same z position of their parent Debris, which is on the same plane as spaceship!
    Therefore… we can ignore the z position of the asteroids themselves when we troubleshoot… it’s Debris we need to make sure is positioned correctly!
    I hope that makes sense… let me know what the transform for Debris is.
  2. The rotation of the spaceship may offset the collider and bullets enough to not hit some of the asteroids.
    The pivot points of these models are not at their centre. I noticed in the screen shot you provided above, if I match your scene view’s orientation in my build, I’m viewing the scene from a different angle. Download the final project and have a look. You’ll see what I mean. I wonder if your game object rotations are a little different to how the tutorial has them set. This could lead to a situation where some of the asteroids when rotated are missed by the bullets fired by the spaceship… I’m afraid this is a best guess as I’m not at my computer and can’t test my theory!
    Let me know how you get on.
    Mark

And one more thing I’m noticing, maybe this is just nothing, but when the asteroids generate, it looks to me like the sphere collider and does not generate where the asteroid is. Again, this doesn’t happen consistently, but I got a screenshot of one instance of Asteroid with what looks like the sphere collider (the green sphere?) and then the controls in two different spots, and not centered on the asteroid.

Hi Haole,
Thank you for persisting with this.

Please have a look at your Asteroid prefab in the Prefabs folder. Click the disclosure arrow beside it to reveal rockLP001. This is the Game Object that contains the asteroid’s mesh renderer, and as you will see, it has a transform or it’s own. Please make sure this transform is set to 0, 0, 0 for both the position and rotation and 1, 1, 1 for the scale. Any alteration to these values will offset the rendered asteroid from the Collider.

Let me know how you get on!

Mark

I had an issue with a part of the script when shooting a bullet, I wasn’t able to actually spam the button. The moment I pushed the spacebar button to shoot a projectile my spaceship was actually disappearing until the bullet flew off screen which was extremely confusing, not to mention the bullet never actually got destroyed. After scowering through the codes I found a fix by making a new variable in the bullet script and adding an update() function to replace the onBecameInvisible() function

using UnityEngine;
using System.Collections;

public class Bullet : MonoBehaviour {

    // new variable to allow bullet on screen for specified time
private float timer = 1;

/**
 * Remove Bullet when it leaves the screen
 */
void Update() {
	timer -= Time.deltaTime;
	if (timer <= 0) {
		Destroy (gameObject);
	}
}
    // I decided to leave this here because Well it didn't make a difference to be honest
void OnBecameInvisible() {
	Destroy (gameObject);	
}

}

Hope this helps anyone who was having the same issue as me! :slight_smile:

I am a beginner and I had to set the depth of Camera to not less than 0 to see the Game preview correctly because the default value was set to -1.

This tutorial is more than six months old, so questions regarding it are no longer supported for the moment. We will update it as soon as possible. Thank you! :]