Scripting error


I am creating a 2d game for Android game.

I am getting this error.

and I am using this code.

using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour {

	public Camera cam;
	public GameObject apple;
	private float maxWidth;
	// Use this for initialization
	void Start () {
		if (cam == null) {
		cam = Camera.main;
		Vector3 upperCorner = new Vector3     (Screen.width, Screen.height, 0.0f); 
		Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
		float appleWidth = apple.this.GetComponent<Renderer>().bounds.extents.x; 
		maxWidth = targetWidth.x - appleWidth;
		StartCoroutine (Spawn ());

	IEnumerator Spawn () { 
		yield return new WaitForSeconds (2.0f);
			while (true) {
			Vector3 spawnPosition = new Vector3 (
			Random.Range (-maxWidth, maxWidth), 
			Quaternion spawnRotation = Quaternion.identity;

			Instantiate (apple, spawnPosition, spawnRotation);
			yield return new WaitForSeconds (Random.Range (1.0f,2.0f));



Exactly as the error say, you don’t have a renderer component attached to the gameobject, so you can’t access it with GetComponent.

I suspect it’s because you meant to access the renderer on the apple gameobject, not on this gameobject. So line 20 shoule be:

float appleWidth = apple.GetComponent<Renderer>().bounds.extents.x;