- Home /
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);
}
}
}
}
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.
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
kk, it's line 37 in the full code. "pre.parent = transform.parent;"
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.
well then either pre
is null, or transform
doesn't have a parent.
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?
Your answer
Follow this Question
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