Unity Games - Chapter 3: Components (Pg. 72-73)

Creating Your First Script

It’s showtime!
You have many options for creating a script. You could click the Add Component button and then select New Script.
But I’d like you to try it this way: select the Scripts folder in the Project Browser, and then click the Createbutton. Select C# Script from the drop-down and name it PlayerController.
You’ll see your new script in the Scripts folder. Drag it from the Scripts folder onto the SpaceMarine GameObject.
You should now see the script listed as one of the components on the Space Marine:

You’ve added your first custom component! Granted, it doesn’t do anything…yet.
You’ll change that in just a moment, but before you do, you need to learn about the Input Manager.


  • I am having an issue with a part of the book:
    We cannot seem to move the script from the script folder into the SpaceMarine GameObject. Do you have any suggestions.

It says: The associated script cannot be loaded. Please fix any compile errors and assign a valid script.

Hi @haturner :smile:

It sounds like there might be an error or formatting issue with the script itself.

Would you be able to attach it as a file to this thread so that I could take a look at it? If Unity is not able to parse and make sense of the script because of an error somewhere within, then that cause be a valid cause for the error you’re seeing.

Thanks!
Sean

@shogan
Print Screen of Script Errors:

Tried to Fix but still have 1 error:

Straight from the book:

Hi @haturner

Thanks! I see there are a number of formatting issues in your script.

For example, you can’t pass parameters into the Update() method like that, and also your Update() method had been moved outside of the PlayerController.cs script class, which is not valid C# code.

Not to worry though, here is how it should look correctly formatted. Just copy/paste from this: PlayerController.cs · GitHub

Make sure you leave the three ‘using’ statements at the top of your existing script though, as I didn’t include those in the gist pasted above. i.e. these ones should stay at the top of your script:

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

Hope that helps!

Sean

@shogan

So we did what you suggested and we still have compiler errors. Script we have put in:

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

public class PlayerController : Monobehaviour
{
public float moveSpeed = 50.0f;

    // Use this for initialization
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 pos = transform.position;
        pos.x += moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
        pos.z += moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
        transform.position = pos;
    }
}

Interesting. Can you send me a screenshot of your IDE / code editor with the script in that shows the compiler errors? Or screenshots of any other areas in the Unity editor showing an issue with the script? What would also be useful is if you could zip/compress the PlayerController.cs file you have right now and attach it to this thread.

Thanks!

@shogan Interestingly we duplicated the information in the PlayController.cs file and it did work. However, I am not have an additional issue with the C# for the

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

public class GameManager1 : MonoBehaviour {
public GameObject player;
public GameObject[] spawnPoints;
public GameObject alien;

public int maxAliensOnScreen;
public int totalAliens;
public float minSpawnTime;
public float maxSpawnTime;
public int aliensPerSpawn;

private int aliensOnScreen = 0;
private float generatedSpawnTime = 0;
private float currentSpawnTime = 0;


// Use this for initialization
void Start () {
	
}

// Update is called once per frame
void Update ()
{
	currentSpawnTime += Time.deltaTime;
	if (currentSpawnTime > generatedSpawnTime) {
	}
	currentSpawnTime = 0;
	generatedSpawnTime = Random.Range (minSpawnTime, maxSpawnTime);
	if (aliensPerSpawn > 0 && aliensOnScreen < totalAliens) {
		{
			if (aliensPerSpawn > spawnPoints.Length) {
			}
			aliensPerSpawn = spawnPoints.Length - 1;
		}
		aliensPerSpawn = (aliensPerSpawn > totalAliens) ? aliensPerSpawn - totalAliens : aliensPerSpawn;
		for (int i = 0; i < aliensPerSpawn; i++) {
			if (aliensOnScreen < maxAliensOnScreen) {
				aliensOnScreen += 1;
				//code goes here
			
				//1
				int SpawnPoint = -1;
				//2
				while (SpawnPoint == -1) { 
					//3
					int randomNumber = Random.Range (0, spawnPoints.Length - 1);
					//4
					if (!previousSpawnLocations.Contains (randomNumber)) {
						previousSpawnLocations.Add (randomNumber);
						SpawnPoint = randomNumber;
					}
				}
				GameObject spawnLocation = spawnPoints [spawnPoint];
				GameObject newAlien = Instantiate (alien) as GameObject;
				newAlien.transform.position = spawnLocation.transform.position;
			}
		}
	}
}

}

We have duplicated exactly from the textbook.

@haturner

It seems like you’re missing a variable declaration.
previousSpawnLocations isn’t declared anywhere, that’s why you’re getting that error.

I took a look at the original code and it seems you forgot to add this line:

    List<int> previousSpawnLocations = new List<int>();

Right above:

    if (aliensPerSpawn > spawnPoints.Length) {

That should fix it!

Cheers!

@blackdragonbe - true that did fix that issue. Thank you so much. The last issue I think…

We are getting this error: The name SpawnPoint does not exist in the current context:

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

public class GameManager1 : MonoBehaviour {
public GameObject player;
public GameObject[] spawnPoints;
public GameObject alien;

public int maxAliensOnScreen;
public int totalAliens;
public float minSpawnTime;
public float maxSpawnTime;
public int aliensPerSpawn;

private int aliensOnScreen = 0;
private float generatedSpawnTime = 0;
private float currentSpawnTime = 0;


// Use this for initialization
void Start () {
	
}

// Update is called once per frame
void Update ()
{
	currentSpawnTime += Time.deltaTime;
	if (currentSpawnTime > generatedSpawnTime) {
	}
	currentSpawnTime = 0;
	generatedSpawnTime = Random.Range (minSpawnTime, maxSpawnTime);

	if (aliensPerSpawn > 0 && aliensOnScreen < totalAliens) {List<int> previousSpawnLocations = new List<int> ();
		{

			if (aliensPerSpawn > spawnPoints.Length) {
			}
			aliensPerSpawn = spawnPoints.Length - 1;
		}
		aliensPerSpawn = (aliensPerSpawn > totalAliens) ? aliensPerSpawn - totalAliens : aliensPerSpawn;
		for (int i = 0; i < aliensPerSpawn; i++) {
			if (aliensOnScreen < maxAliensOnScreen) {
				aliensOnScreen += 1;
				//code goes here
			
				//1
				int SpawnPoint = -1;
				//2
				while (SpawnPoint == -1) { 
					//3
					int randomNumber = Random.Range (0, spawnPoints.Length - 1);
					//4
					if (!previousSpawnLocations.Contains (randomNumber)) {
						previousSpawnLocations.Add (randomNumber);
						SpawnPoint = randomNumber;
					}
				}
				GameObject spawnLocation = spawnPoints [spawnPoint];
				GameObject newAlien = Instantiate (alien) as GameObject;
				newAlien.transform.position = spawnLocation.transform.position;
			}
		}
	}
}

}

Case sensitive issue. spawnPoint vs. SpawnPoint

This topic was automatically closed after 166 days. New replies are no longer allowed.