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 Caiuse · Feb 04, 2011 at 05:25 PM · javascript

"Object reference not set to an instance of an object" This should work but it doesn't

var buildingTypes : GameObject[]; var buildIndex : int = 0; var notHere : boolean = true; var buildingHere : boolean = false;

function Update () { if(Input.GetMouseButton(0)){ //Check to see if there is a building checkForBuilding(); //If there isn't a building make one if(!buildingHere){ makeBuild(); //If there is a building destroy it }else{ destroyBuild(); } } }

//Check to see if there is a building function checkForBuilding() { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (hit.collider.gameObject.CompareTag("buildingTypes")){ //Tell unity there there is a building buildingHere = true; Debug.Log("One here"); } }

//Destroy Building function destroyBuild() { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit, 100) && hit.collider.gameObject.CompareTag("buildingTypes")){ Debug.DrawLine (ray.origin, hit.point); Destroy(hit.collider.gameObject); } }

//Make building function makeBuild(){ var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit, 100) && hit.collider.gameObject.CompareTag("terrain")){ Debug.DrawLine (ray.origin, hit.point); Instantiate(buildingTypes[buildIndex],hit.point,Quaternion.identity); } }

Instead of the makeBuild(); function being run it throws up a debug log error "Object reference not set to an instance of an object" i can't see how I can reference an object that doesn't yet exists (although it is a prefab with the tag "buildingTypes")

Thanks - C

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 DaveA · Feb 04, 2011 at 05:50 PM

Assuming you have assigned some GameObjects in the Inspector to that array buldingTypes, check the buildIndex for out-of-bounds. If you have 3 elements, and the buildIndex is 3, it will fail like that (0,1,2 ok)

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 Caiuse · Feb 08, 2011 at 11:10 AM 0
Share

I'm still not completely with you, I've started by only assigning one GameObject in the hope this would be a simple process and allow me to in future add more GameObjects. Even with that the case and the buildIndex set to 1 (tried 0) I still get an error!

avatar image DaveA · Feb 08, 2011 at 10:57 PM 0
Share

One object assigned, the index must only be 0. If that's the case, I'd hit the debugger and/or put lots of Debug.Log statements in to ensure that code is executing as planned.

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

No one has followed this question yet.

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Realistic Bouncing Effect 1 Answer

Why does my cube rotate? 0 Answers

Set a Prefab's variable when you instantiate it, but before it calls Awake? 2 Answers


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