Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FullMe7alJacke7 · Apr 24, 2015 at 06:03 PM · camera viewport

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;
             }
         }
     }
 }
 


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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>();
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FullMe7alJacke7 · Apr 24, 2015 at 10:35 PM 0
Share

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

avatar image FullMe7alJacke7 · Apr 25, 2015 at 09:51 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

Set Camera behind player without making it a child 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges