- Home /
Anyone know what's wrong with this code? (SOLVED)
I keep getting a error on this code. It says: 'isBuild' is not a member of 'UnityEngine.Component'. it occurs on line 15 For some reason I can't get a straight answer, Thanks if you know.
#pragma strict
var Prefab : Transform;
function Start () {
}
function Update () {
}
function OnGui () {
if (GUI.Button (Rect (10,10,150,100), "Create")) {
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("SetObject");
for (var g in gos){
if (g.GetComponent("here the name of the Script").isBuild);
Instantiate(Prefab, g.transform.position, Quaternion.identity);
Application.Quit();
}
}
}
Try
I cannot type the proper brackets on mobile lol Look here http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html
You have to define the type like in the second example
I edited my comment sorry ;) you have to use the 'bihger than' and 'smaller than' brackets which i cannot type on mobile.
GetComponent.+scriptName+() Ins$$anonymous$$d of the + use smaller than and bigher than symbols Hope you understand
Answer by Graham-Dunnett · Jul 04, 2013 at 09:52 PM
GetComponent()
returns a Component
. That's a Unity base class. When you create a script, it inherits from Component
. The script you create has extra members, of course, so, in your example, it looks as though your script has a member called isBuild
. When you use GetComponent()
to find your script, you can do:
var s : YourScript = g.GetComponent(YourScript) as YourScript;
if (s.isBuild) {
//blah
This grabs the component, and casts it to YourScript
, which has the isBuild
member. Alternatively you can use the generic version:
var s : YourScript = g.GetComponent.<YourScript>();
if (s.isBuild) {
//blah
which does the exact same thing. Obviously you'd check that s
has a value before using it.
Answer by trs9556 · Jul 04, 2013 at 09:46 PM
I don't use JS and I'm going on a limb here but try something along the lines of:
if (GUI.Button (Rect (10,10,150,100), "Create")) {
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("SetObject");
for (var g in gos){
var temp : ScriptName;
temp = g.GetComponent("here the name of the script");
if(temp.isBuild()){
Instantiate(Prefab, g.transform.position, Quaternion.identity);
}
Application.Quit();
}
}
Answer by uberokeer · Jul 04, 2013 at 10:25 PM
Okay I have another problem :p My Script won't work at all! xD I have it applied to my camera The whole Script is:
var Prefab : Transform;
function OnGui () {
if (GUI.Button(Rect(10,10,50,50),"Create")){
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("SetObject");
for (var g in gos){
if (g.GetComponent.<ControlObject>().isBuild);
Instantiate(Prefab, g.transform.position, Quaternion.identity);
}
}
}
I don't see anything wrong with it... I even tested to see if it was working by putting: 'Application.Quit();'
in the start function and it still didn't do anything :/ once again i have this script on my camera. Anyone have a idea on what may be causing this? Thanks if you can help! :D
I think 'OnGui' should be 'OnGUI'. Also, there's a semicolon at the end of line 7, watch out for that! :)
Yeah that's actually what the problem was. I resolved it earlier. Thanks though :)
Answer by testandi · Jul 05, 2013 at 01:02 PM
can you see in the inspector that when running the scene you have an object with tag "SetObject"?
Im editing my own answer now as somebody said it is not an answer but a comment
it could be that the code is calling a tag which is not existing.
That's not an answer! If you want to post a comment, use the "add new comment" button below the post you want to comment on.
Answers should answer the question.
Well i got everything to work, I found out what the problem was, that i spelled it 'OnGui' ins$$anonymous$$d of 'OnGUI'
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
BCE0044: expecting EOF, found '}'. 1 Answer
Assets/cubeAI.js(2,11): UCE0001: ';' expected. Insert a semicolon at the end. 1 Answer
Picking up an object 1 Answer
SendMessage illogical errors 0 Answers