• 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 Billamu · Sep 23, 2010 at 06:50 AM · camerascreentoworldpointmain

Why won't my script see the main camera?

This script is in a prefab. When I run the script, Unity says... "NullReferenceException: Object reference not set to an instance of an object"

function StartAtARandomSpot( zdepth : float )
{
    var campos : Vector3 = Vector3(Random.Range(0,scrWidth), scrHeight+80, zdepth);
    campos = Camera.main.ScreenToWorldPoint (campos);
    transform.position = campos;
    originalSpawnPoint = transform.position;    
}   

Any help would be greatly appreciated.

Comment
Add comment · Show 1
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 · Sep 23, 2010 at 07:09 AM 0
Share

I've assumed the error is pointing to the line where you're calling 'Camera.main.ScreenToWorldPoint(campos);' If that's incorrect, please update your question to clarify! Cheers.

1 Reply

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

Answer by · Sep 23, 2010 at 07:07 AM

Make sure that your camera is tagged as MainCamera, or "Camera.main" won't work. Otherwise, I've verified that your script is fine. Although, you could simplify it to:

function StartAtARandomSpot( zdepth : float )
{
    var campos : Vector3 = Vector3(Random.Range(0,scrWidth), scrHeight+80, zdepth);
    originalSpawnPoint = transform.position = Camera.main.ScreenToWorldPoint (campos);
}

(Note that you don't even need the campos variable, but for readability you could easily justify keeping it)

Comment
Add comment · Show 4 · 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 Billamu · Sep 23, 2010 at 07:12 AM 0
Share

Didn't realise you could double up your '='s there. Nice. $$anonymous$$y camera is tagged as $$anonymous$$ainCamera, but sadly the bug still remains.

avatar image · Sep 23, 2010 at 07:30 AM 0
Share

Are you sure that the error is about the ScreenToWorldPoint line? If you comment it out, does the error still occur? If I put this code into an empty script on a new object, it works fine.

avatar image Billamu · Sep 23, 2010 at 08:35 AM 0
Share

I've been tinkering with things and I've found that the prefabs were originally in the Heirachy; I was using an object pool on them. I've found that when I revert to instantiating the prefabs the problem goes away.

avatar image · Sep 23, 2010 at 08:40 AM 0
Share

Glad you solved it - sorry that I couldn't help, beyond telling you that your script was fine!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

2.5d camera control script 1 Answer

Camera Toggle script 1 Answer

What makes Camera.ScreenToWorldPoint return different results with same input? 0 Answers

Camera.main.ScreenToWorldPoint works within Unity editor, but not in builds 0 Answers

changing the target of camera through script 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