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 chrisall76 · Jul 21, 2013 at 10:25 AM · guigameobjectnullreferenceexception

Setting parent gives error?

Hello, I'm trying to set the parent of a instantiated gameobject. It gives this error:

 NullReferenceException: Object reference not set to an instance of an object

Heres the code giving there error, line 37:

     void OnGUI(){
         if(showGUI == true && AbleToPlace == true){
             Transform pre;
             if (GUI.Button(new Rect(45, 70, 50, 30), Towers[0].name) && cam.GetComponent<numberKeeper>().UnitPoints - TowerPrices[0] >= 0){
                 pre = Instantiate(Towers[0], transform.position, Quaternion.identity) as Transform;
                 cam.GetComponent<numberKeeper>().ChangeUnit(-25);
                 pre.parent = transform.parent;
                 Destroy(gameObject);
             }
         }
     }

Heres the full script:

 using UnityEngine;
 using System.Collections;
 
 public class towerPlacement : MonoBehaviour {
     
     private GameObject cam;
     private bool AbleToPlace = true;
     private bool showGUI = false;
     
     public GameObject[] Towers; 
     public int[] TowerPrices;
     
     // Use this for initialization
     void Start () {
         cam = Camera.main.gameObject;
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetButtonDown("Fire2")){
             showGUI = false;
         }
     }
     
     void OnMouseOver() {
         if(Input.GetButtonDown("Fire1")){
             showGUI = !showGUI;
         }
     }
     
     void OnGUI(){
         if(showGUI == true && AbleToPlace == true){
             Transform pre;
             if (GUI.Button(new Rect(45, 70, 50, 30), Towers[0].name) && cam.GetComponent<numberKeeper>().UnitPoints - TowerPrices[0] >= 0){
                 
                 pre = Instantiate(Towers[0], transform.position, Quaternion.identity) as Transform;
                 pre.parent = transform.parent;
                 
                 cam.GetComponent<numberKeeper>().ChangeUnit(-25);
                 Destroy(gameObject);
             }
         }
     }
     
 }
 
Comment
Add comment · Show 2
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 clunk47 · Jul 21, 2013 at 05:08 PM 0
Share

Post the whole code. I don't know what the Towers[] array is or how it is defined, or why you are defining pre locally in the GUI code. Someone may be able to help without it, but I can usually figure things out easier if you post the whole script. If you don't want to do that, at least give us the LINE that the error is showing you.

avatar image chrisall76 · Jul 21, 2013 at 10:47 PM 0
Share

Added full script

2 Replies

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

Answer by Peter G · Jul 21, 2013 at 11:21 PM

It could be one of a few things depending on the exact line number of the error (you should be able to see the exact line from the error).

 if (GUI.Button(new Rect(45, 70, 50, 30), Towers[0].name) && 
    cam.GetComponent<numberKeeper>().UnitPoints - TowerPrices[0] >= 0) 

If you don't have a numberKeeper attached to your cam then you'll get a Null Reference.

 pre = Instantiate(Towers[0], transform.position, Quaternion.identity) as Transform;

This one's a little more subtle. Using as to cast has a little caveat about it. Unlike like (type) casting, using as will return null if you can't make the cast. This shouldn't be your problem, but it's a handy fact to know.

 cam.GetComponent<numberKeeper>().ChangeUnit(-25);

If cam is null or it doesn't have a`numberKeeper` attached to it, then you'll get a null reference.

 pre.parent = transform.parent;
 

If pre is null as stated above, this could give you a null reference. Or also possible, there's no parent game object on this script.

Those would appear to be the most likely candidates for the error. I'd try all of the above and see if that solves your problem. And if you can give us the specific line that would narrow down the search.

-Peter G

Comment
Add comment · Show 3 · 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 chrisall76 · Jul 21, 2013 at 11:46 PM 0
Share

kk, it's line 37 in the full code. "pre.parent = transform.parent;"

avatar image clunk47 · Jul 22, 2013 at 12:00 AM 0
Share

Try transform.parent.transform. Also make sure pre isn't null, which looks like pre would be Towers[0]. This is not empty in the inspector? As well, make sure that (this) transform actually has a parent.

avatar image Peter G · Jul 22, 2013 at 05:34 PM 0
Share

well then either pre is null, or transform doesn't have a parent.

avatar image
0

Answer by IgorAherne · Jul 21, 2013 at 12:20 PM

The code is quite neat.

Perhaps, you need

(the type of UnitPoints) cam.GetComponent().UnitPoints

added in your if check?

Comment
Add comment · 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

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using GUI and check what button was pressed 1 Answer

how to call a gameobject in one function(mouse selection) to another function (GUI) 1 Answer

Game Object enable for game mode selection on GUI 1 Answer

NullReferenceException while trying to access a Component 4 Answers

GUI Window on GameObject location? 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