- Home /
Having trouble linking camera in my script?
Hello, my game objects "formations" are not moving properly... They move to the right and get stuck. I am getting the error
MissingComponentException: There is no 'Camera' attached to the "EnemySpawners" game object, but a script is trying to access it.You probably need to add a Camera to the game object "EnemySpawners". Or your script needs to check if the component is attached before using it.FormationController.Start () (at Assets/Enemies/FormationController.cs:17)
I tried changing camera to Camera, but that did not help I am rather confused on how to link the same camera in different scripts, because right now the camera is attached to my player sprite and follows the ship around in a different script.
Here is my code,
using UnityEngine;
using System.Collections;
public class FormationController : MonoBehaviour
{ //Spawning Variables
public GameObject EnemyPrefab;
public float W = 10, H = 5;
//Movement Variables
public float Speed = 5;
private int Direction;
private float BoundaryRightEdge, BoundaryLeftEdge;
public float Padding = 0.25f;
void Start() //Setting Boundary
{ Camera camera = GetComponent<Camera>();
float distance = transform.position.z - camera.transform.position.z;
BoundaryLeftEdge = camera.ViewportToWorldPoint(new Vector3(0, 0, distance)).x + Padding;
BoundaryRightEdge = camera.ViewportToWorldPoint(new Vector3(1, 1, distance)).x - Padding;
//Spawn at each Enemy Formation Game Object
foreach (Transform child in transform)
{
GameObject enemy = Instantiate(EnemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
enemy.transform.parent = child;
}
}
void OnDrawGizmos()
{
float xmin, xmax, ymin, ymax;
xmin = transform.position.x - 0.5f * W;
xmax = transform.position.x + 0.5f * W;
ymin = transform.position.y - 0.5f * H;
ymax = transform.position.y + 0.5f * H;
Gizmos.DrawLine(new Vector3(xmin, ymin, 0), new Vector3(xmin, ymax, 0));
Gizmos.DrawLine(new Vector3(xmin, ymax, 0), new Vector3(xmax, ymax, 0));
Gizmos.DrawLine(new Vector3(xmax, ymax, 0), new Vector3(xmax, ymin, 0));
Gizmos.DrawLine(new Vector3(xmax, ymin, 0), new Vector3(xmin, ymin, 0));
}
void Update()
{ float FormationRightEdge = transform.position.x + 0.5f * W;
float FormationLeftEdge = transform.position.x - 0.5f * W;
if (FormationRightEdge > BoundaryRightEdge)
{
Direction = -1;
}
if (FormationLeftEdge < BoundaryLeftEdge){
Direction = 1;
}
transform.position += new Vector3(Direction * Speed * Time.deltaTime, 0, 0);
//Spawn Enemies on Keypress
if (Input.GetKeyDown(KeyCode.S))
{
foreach (Transform child in transform)
{
GameObject enemy = Instantiate(EnemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
enemy.transform.parent = child;
}
}
}
}
Answer by hbalint1 · Apr 24, 2015 at 06:18 PM
So the problem is what it says. There is no Camera component attached to your EnemySpawner GameObject. If you want to reference your player's camera in that line, then you should change line 16. to:
Camera camera = GameObject.Find("yourPlayer'sName").GetComponent<Camera>();
Thank you, I actually changed it to
Camera camera = GameObject.Find("Player").GetComponentInChildren<Camera>();
Because the Camera is a Child of my Player game object, that was the only way I could figure out how to get my script to work for the player script, but that is something for me to figure out some other time... Sorry for the dumb questions I have the problem of knowing what code I need to write but not knowing how it should be written if that makes sense?
Now I believe I have my code set up wrong because it pretty much infinitely spawns enemies until the game crashes. Solve one problem and I find 2 more lol
Thank you, I actually changed it to Camera camera = GameObject.Find("Player").GetComponentInChildren(); Because the Camera is a Child of my Player game object, that was the only way I could figure out how to get my script to work for the player script, but that is something for me to figure out some other time... Sorry for the dumb questions I have the problem of knowing what code I need to right but not knowing how it should be written if that makes sense?
Now I believe I have my code set up wrong because it pretty much infinitely spawns enemies until the game crashes. Solve one problem and I find 2 more lol
Your answer
Follow this Question
Related Questions
Is it possible to check what percentage of an object is visible in a camera's view? 0 Answers
make every player see the same picture with different aspect ratios? 2 Answers
Trying to adjust camera for different resolutions cuts out part of the image 2 Answers
How to properly set camera sizes in 2D Ortho camera split screen? 0 Answers