Week 4

score bulb in Unity scene
link

Making a Score Light that lights up when you get a snitch.

The Easy stuff:
The Tricky stuff:

Send values between scripts

There are a few ways to send the score value between scripts. This is the most obvious one.
get score value from player script
	using System.Collections;
	using System.Collections.Generic;
	using UnityEngine;
	
	public class lightActivate : MonoBehaviour {
	
		public GameObject player; 
	
		void Update () {
			snitchCollision tempScript = player.GetComponent<snitchCollision>();
			if(tempScript.score >= 1){ // score must be a global variable
				Degug.Log("turn light on")
			}
		}
	}
 
You don't actually have to make an instance of the script if you just need one or two values. This is a little simpler and works just as well.
Slightly Faster Way
	if(player.GetComponent<snitchCollision>().score >= 1){
		Debug.Log("socre");
	}
 

Static Variable

Another method of sharing values across scripts

If you make your variable static public that variable is shared across all instances of a code. This way, even if the code is on a prefab that has dozens of instances, the variable is "global" across all the objects.
Another useful advantage of static variables is that it is easy to address them from other scripts, you just need the name of the script and the variable. No need to get the GameObject that the script is attached to.
In the snitchCollision script make the score public with
static public int score;
To check score variable in the code on the light simply write
if(snitchCollision.staticscore >= 1){
You'll notice that making a variable static means it no longer shows up in the inspector. This makes sense when you think about it. The variable no longer exists for just one instance of the class, it exists for all the classes. If you really need to see the variable, print it with Debug.Log, or make a separate variable public variable and set it to the value of the serial variable.

Setting a value in a Child

Yes, there are other ways we could have turned on the light, but learning that you can change values in children is important too.
getting a component in a child
	using System.Collections;
	using System.Collections.Generic;
	using UnityEngine;
	
	public class lightActivate : MonoBehaviour {
	
		public GameObject player; 
		private Light lt;
	
		void Start () {
			lt = GetComponentInChildren(); 
		}
	
		void Update () {
			snitchCollision tempScript = player.GetComponent<snitchCollision>();
			if(tempScript.score >= 1){ // score must be a public variable
				lt.intensity = 2; 
			}
		}
	}
 

partical systems happy face

Particle systems

You can create a particle system and place it into the scene by going Game Objects - Effect - Partical System. Pull it into the project folder to make it a prefab and once it's a prefab you can instantiate it just like you can with any other prefab. Uncheck the loop check-box if you want it to die off over time!
There are a lot of different ways you can effect the particles. Mostly you can explore and learn about those on your own, but here's the best way to change the default shape of the particles to a PNG.
particle Material box

particle Renderer box

Audio

If you look at the camera in the inspector it has an audio "listener" already attached to it. There can be only one audio listener in a scene, so if you add more it will bark at you. These are your ears (or your microphone), so it makes sense they go on the camera. You can, however, have multiple sources.

Playing one sound effect

The source component still need to exist on the object as well as attaching a sound to both the source and this script in the inspector.
Play one sound
   using UnityEngine;
   using System.Collections;
   
   public class gateAudioReward : MonoBehaviour
   {
       public AudioClip gateGood;
       private AudioSource source;
       void Start()
       {
           source = GetComponent <AudioSource >(); // make sure you have an audio source attached as well
       }
   
       void OnTriggerEnter(Collider player)
       {
           if (player.name == "[CameraRig]")
           {
               source.PlayOneShot(gateGood, 1.0f);
           }
       }
   }
 
Play sound from an array

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

public class musicPlayer : MonoBehaviour {
    public AudioClip [] playlist;
    public AudioSource source;
    public int playNum; 

    // Use this for initialization
    void Start () {
       // playlist = new AudioClip[10];
    }
	
	// Update is called once per frame
	void Update () {
        if (source.isPlaying)
        {
            return;
        }
        source.PlayOneShot(playlist[playNum], 1.0f);
        playNum++; 
        if (playNum >= playlist.Length)
        {
            playNum = 0; 
        }
    }
}
 

Audio with the Mixer

Instead of sending the sound to the default listener you can send it first to a mixer that lets you play with and tweak all the sounds in your project. You can make a mixer by going under the create menu in the project area and selecting "Audio Mixer". The mixer should now have a tab in the project area. To create a new channel click on the "+" symbol next to groups. Now go back to the audio source for your sound and select the channel to send the sound to.
unlike the rest of unity, changing sounds in the mixer while it's playing will stay changed when you stop.

Mixer control with code

Left click on the channel you want to control.
In the inspector right click on the effect you want to modify (probably volume) and select “expose variable”. This is a weird interface design, but there you have it.
Back in the mixer select the “exposed parameters” pull-down menu (top right) and rename it something useful.
In the code, make sure you include the audio library with
using UnityEngine.Audio;
Make a reference to the mixer in the declaration area with
public AudioMixer masterMixer;
DON'T forget to then drag the mixer to this script in the inspector! When you want to set the volume in the code write
masterMixer.SetFloat("yourChannelName", -80); // off
masterMixer.SetFloat("yourChannelName", 0); // on