- Home /
null reference exception on get component
The code is giving me a null reference error and i dont understand why? The error is apparently on the 2nd line of ProjectileEquip function. been trying to figure it out for far to long.
using UnityEngine;
using System.Collections;
public class SubMenuGUI : MonoBehaviour {
public GUIStyle myGUI;
public int selGridInt = 0;
public string[] weapons = new string[] {"Plasma", "RailGun"};
private scriptPlayer scriptplayer;
void Awake(){
scriptplayer = GetComponent<scriptPlayer>();
}
void OnGUI(){
GUI.Box(new Rect(0,0,Screen.width,Screen.height),"Customize Your Ship", myGUI);
selGridInt = GUI.SelectionGrid(new Rect(25, 25, 150, 30), selGridInt, weapons, 2);
ProjectileEquip();
if(GUI.Button(new Rect(Screen.width / 2 - 50, Screen.width / 2 + 150,100, 50), "Enter World"))
Destroy(this);
}
void ProjectileEquip(){
if(selGridInt == 0){
scriptplayer.plasma = true;
}
if(selGridInt == 1)
print("RailGun");
}
}
Answer by DeepakSingh491 · Nov 09, 2013 at 05:54 PM
Hey! its not getting the right script check that you have script name right..:)
Answer by DeepakSingh491 · Nov 09, 2013 at 05:54 PM
int the script getcomponent you should have written getcomponent(script)
Answer by Newbyscript · Nov 09, 2013 at 06:25 PM
the name of the script is scriptPlayer
(its not letting me add comment so i had to say in answers)
Answer by vikitosss · Oct 11, 2020 at 05:14 PM
How to Get 2 different Components to one variable ? srcFirst.cs / srcSecond.cs
public ????? src;
void start(){
src = gameObject.GetComponent<srcFirst>();
if(!src) src = gameObject.GetComponent<srcSecond>();
}
I get "Embedded statement cannot be a declaration or labeled statement" But I need only one variable..