Spawn error

I don’t know why these errors pop up, I am using unity 4.5.2
[34237-spawn+error.png|34237]

here is the code


using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour 
{
	public Camera cam;
	public GameObject egg;

	private float maxWidth;
	
	void Start () 
	{
		if (cam == null) 
		{
			cam = Camera.main;
			Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
			Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
			float eggWidth = egg.renderer.bounds.extents.x;
			maxWidth = targetWidth.x - eggWidth;
			StartCoroutine (Spawn ());
		}
	

		IEnumerator Spawn() // error
		{
			yield return new WaitForSeconds(2.0f);
			while (true) 
			{
			Vector3 spawnPosition = new Vector3 (
				Random.Range(-maxWidth,maxWidth), 
				transform.position.y,            
				0.0f                              
				);
			
			Quaternion spawnRotation = Quaternion.identity; 
			Instantiate (egg, spawnPosition, spawnRotation);
			yield return new WaitForSeconds (Random.Range(1.0f, 2.0f));
			}
		}

	}
} //error

You don’t close function Start(). I change full your script:

 public Camera cam;
 public GameObject egg;

 private float maxWidth;
 
 void Start () {
  if (cam == null)  {
   cam = Camera.main;
   Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
   Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
   float eggWidth = egg.renderer.bounds.extents.x;
   maxWidth = targetWidth.x - eggWidth;
   StartCoroutine (Spawn ());
  }
 } //close function

 IEnumerator Spawn() {
  yield return new WaitForSeconds(2.0f);
  while (true) {
   Vector3 spawnPosition = new Vector3 (Random.Range(-maxWidth,maxWidth), transform.position.y, 0.0f);

   Quaternion spawnRotation = Quaternion.identity; 
   Instantiate (egg, spawnPosition, spawnRotation);
   yield return new WaitForSeconds (Random.Range(1.0f, 2.0f));
  }
 }

Check all “{” and “}”. They count is match. I hope that it will help you.

i am faceing an error of renderer.bounds.extents help me here any one?

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

public class GameController : MonoBehaviour {
public Camera cam;
public GameObject egg;

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 eggwidth = egg.renderer.bounds.extents.x; // error
	maxwidth = targetwidth.x - eggwidth;
	StartCoroutine (Spawn ());   
}
IEnumerator Spawn(){
	yield return new WaitForSeconds(2.0f);
	while (true) {
		Vector3 spawnPosition = new Vector3 (Random.Range(-maxwidth,maxwidth), transform.position.y, 0.0f);
		Quaternion spawnRotation = Quaternion.identity; 
		Instantiate (egg, spawnPosition, spawnRotation);
		yield return new WaitForSeconds (Random.Range(1.0f, 2.0f));
	}

}

,using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour
{
public Camera cam;
public GameObject egg;

private float maxWidth;

void Start () {
	if (cam == null)  {
		cam = Camera.main;
		Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
		Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
		float eggWidth = egg.renderer.bounds.extents.x; //error
		maxWidth = targetWidth.x - eggWidth;
		StartCoroutine (Spawn ());
	}
} //close function

IEnumerator Spawn() {
	yield return new WaitForSeconds(2.0f);
	while (true) {
		Vector3 spawnPosition = new Vector3 (Random.Range(-maxWidth,maxWidth), transform.position.y, 0.0f);
		Quaternion spawnRotation = Quaternion.identity; 
		Instantiate (egg, spawnPosition, spawnRotation);
		yield return new WaitForSeconds (Random.Range(1.0f, 2.0f));
	}
}

}