- Home /
BCE0023 No appropriate Version of UnityEngine.GameObject.GetComponent
This is the full Error Message:
Assets/guiHUD.js(59,59): BCE0023: No appropriate version of 'UnityEngine.GameObject.GetComponent' for the argument list '(UnityEngine.GameObject)' was found.
Basically creating a simple game and the section i'm trying to code uses another script to reference from (PlayerDataScript) in this script it holds information for players name, score, items picked up etc. What i'm trying to do in my other script "GUIhud" is first create a HUD but secondly show the players name score and items picked up etc. Basically keep getting the same error for 4 similar lines of code which are basically 4 blocks which refer to going back and looking at the PlayerDataScript and returning the answer. This is a screen shot of the code that is wrong.
Answer by Kiloblargh · Nov 11, 2013 at 09:24 PM
This is the kind of trouble you get into not following the lowercase-for-variables, uppercase-for-scripts convention.
Rename PlayerDataScript and the error will go away.
Example:
var pds : PlayerDataScript = playerData.GetComponent.< PlayerDataScript >();
(I usually use the "generic" version of GetComponent (with the angle brackets) because it always returns the proper type instead of just type Component.)
What you did in the original script is just as bad as having a transform variable named "Transform".
Thank you however i'm completely new to unity and i'm only doing a crash course for a module in university, so would I put this line of code at the beginning of the script with the errors or the beginning of the script that is named PlayerDataScript?
Thanks
function getNumItemsPickedUp() : int{
var pds : PlayerDataScript = playerData.GetComponent.< PlayerDataScript >();
if(playerData != null){
//logic
}
}
You may want the pds var declared at the top, not within the function, so that it sticks around.
Thank you, however i'm completely new to unity and program$$anonymous$$g and it is only a side module at university and i'm not completely sure with how to go about fixing this, would I use this code at the beginning of the script that has the errors or the Script that is called PlayerDataScript
Thanks :)
Answer by escalador · Jun 24, 2017 at 05:29 PM
my problem is Assets/AmmoBox.js(10,51): BCE0023: No appropriate version of 'UnityEngine.GameObject.GetComponent' for the argument list '(UnityEngine.GameObject)' was found.
my script is : #pragma strict
var ammo : int = 30; var eject : GameObject;
private var shoteject : eject; private var showGUI : boolean = false;
function Start () { shoteject = GameObject.Find("eject").GetComponent (eject); }
function Update () { if(showGUI == true){ if(Input.GetKeyDown("e")){ shoteject.reserve += ammo; Destroy (gameObject); } } }
function OnTriggerEnter(hit : Collider){
if(hit.gameObject.tag == "Player"){ showGUI = true; } }
function OnTriggerExit(hit : Collider){
if(hit.gameObject.tag == "Player"){ showGUI = false;
} }
function OnGUI(){
if(showGUI == true){ GUI.Box(Rect(Screen.width/2-100,Screen.height/2-12.5,200,25), "Press E to pickup ammo"); } }
Your answer
Follow this Question
Related Questions
Photon RPC problem... 3 Answers
URGENT - Serializing error The type of argument is not primitive 0 Answers
"Takes `2' arguments" 2 Answers
Argument out of Range 1 Answer