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.
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! 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;
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.
@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;
}
}
}
}