- Home /
Spawn error
I don't know why these errors pop up, I am using unity 4.5.2
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
please tell me what to do??? when i remove the last bracket it says only one error about Spawn(), but then the syntax of the code is not in the proper correct form.
Answer by zharik86 · Oct 24, 2014 at 06:20 PM
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.
O$$anonymous$$G,,,, very very common program$$anonymous$$g mistake how can I forget it. Thank you so much @zharik86
i need a little bit help ! please help me i have renderer problem
Answer by umerslilla · Apr 06, 2018 at 03:46 AM
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));
}
}
}
@umerslilla , You havent defined the renderer property; you need to use GetComponent. declare a Gameobject[] egg; define a public Renderer ren; And in the Start method
ren = egg[0].GetComponent();
Your answer
![](https://koobas.hobune.stream/wayback/20220613165851im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to rotate 90 degrees 4 Answers
A node in a childnode? 1 Answer
Texture2d.Readpixel slow down performance while stand-alone exe is minimize 0 Answers
Fade in Fade out in unity 1 Answer