How can I get this code to recognize coll in my update function?

using UnityEngine;
using System.Collections;

public class Ninja : MonoBehaviour {

float speed = 7.0f;

// Use this for initialization
void Start () {
	transform.position = new Vector3 (2, 8, -5);


// Update is called once per frame
void update () {
	if (coll.collider == false & Input.GetKey (KeyCode.UpArrow)) {
		Debug.Log ("up");


void faceRight () {
	transform.localRotation = Quaternion.Euler (0, 0, 0);
void OnCollisionStay2D(Collision2D coll){
	// If the Collider2D component is enabled on the object we collided with.
	if (coll.collider == true) {
		if (Input.GetKey (KeyCode.LeftArrow)) {
			Debug.Log ("left arrow");
			transform.position += Vector3.left * speed * Time.deltaTime;
		if (Input.GetKey (KeyCode.RightArrow)) {
			Debug.Log ("right arrow");
			transform.position += Vector3.right * speed * Time.deltaTime;



Okay, so the problem here is called Scope. There are a bunch of articles you can look up online if you use that keyword, but here’s a basic breakdown:

Variables ONLY exist within the block that they are defined. In your case, the coll variable is defined in the OnCollisionStay2D function as a parameter, so it can’t be accessed in any other function. If you need to use it in the update function, you need to cache it off as a class-scope variable (like your speed float), which will allow it to be accessed from any function in that class. But keep in mind class variables are persistent with the instance, so if you set it in the OnCollisionStay2D function, it will stay set, and will not revert to null when there isn’t a collision, so you’ll have to handle un-setting it too.