- Home /
Why isn't this script working? (Beginner in coding)
I'm trying to make it so that when the player has destroyed 6 objects, the 6 objects will be noted as "true". This doesnt seem to be working and I can't figure out why. Please help.
var hasDalek : boolean = false;
var hasMeteor : boolean = false;
var hasWire : boolean = false;
var hasCasing : boolean = false;
var hasDalekLight : boolean = false;
var hasPlans : boolean = false;
var Dalekgun : GameObject;
var Meteor : GameObject;
var Wire : GameObject;
var Casing : GameObject;
var DalekLight : GameObject;
var Plans : GameObject;
function Start () {
if (hasDalek)
{
if (Dalekgun == null);
(hasDalek) = true;
}
if (hasMeteor)
{
if (Meteor == null);
hasMeteor = true;
}
if (hasWire)
{
if (Wire == null);
hasWire = true;
}
if (hasCasing)
{
if (Casing == null);
hasCasing = true;
}
if (hasDalekLight)
{
if (DalekLight == null);
hasDalekLight = true;
}
if (hasPlans)
{
if (Plans == null);
hasPlans = true;
}
}
Answer by Kiloblargh · Jun 23, 2013 at 02:01 AM
Because... it's wrong?
Ok, I'm sorry about that. One of those days...
First [ never mind, boolean
will do fine ]
Second, you have unnecessary parentheses on line 21.
Third, variables start with lowercase.
Fourth, you can't put a semicolon after an if statement, you need nested brackets.
Fifth, even if you fix the syntax, nothing will be set to true unless it was already true.
Finally, this script doesn't appear to actually do anything useful, even if it were correctly written.
That about covers it. Mainly, you need to brush up on how if statements work. [edit] Missed your explanation first time. I'll go ahead and fix your code so it works. The other critical failure in your script is that it runs only once, in Start(). It won't run every time you destroy an object, so nothing will get updated.
var hasDalekGun : System.Boolean = false;
var hasMeteor : System.Boolean = false;
var hasWire : System.Boolean = false;
var hasCasing : System.Boolean = false;
var hasDalekLight : System.Boolean = false;
var hasPlans : System.Boolean = false;
var dalekGun : GameObject;
var meteor : GameObject;
var wire : GameObject;
var casing : GameObject;
var dalekLight : GameObject;
var plans : GameObject;
function Awake ()
{
dalekGun = GameObject.Find("TheDalekGunOrWhateverYouCalledIt");
}
function Update ()
{
CheckStuff ();
}
function CheckStuff ()
{
if (!hasDalekGun && !dalekGun)
{
hasDalekGun == true;
}
// etc.
}
But still, I don't think what you are doing is the proper way to go about it. When the item is picked up, right before you destroy it, you can set the appropriate boolean variable to "true". There is no need to keep checking. If it is another script, just use dot notation.
at the start: var nocs : NameOfControlScript; drag on in the Inspector
then at the appropriate point in the script:
nocs.hasDalekGun = true;
var hasCasing : boolean = false;
That is proper for setting boolean variables.
One more : all this is in a Start function, did you mean Update?
I agree with everything in this answer, except the boolean statement confuses me. In uJS
var myBool : boolean = false;
myBool = true;
if ( myBool ) // same as writing -> if ( myBool == true )
{
// Do Stuff
}
This is how to declare, assign and use booleans in unityJavaScript.
Apart from that, agreed, this is a mess.
Here are some links I strongly suggest to all new users :
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/
this is the YouTube link for the above as one playlist : http://www.youtube.com/watch?v=-oXYHNSmTxg&list=PL27B696FB515608D2&feature=plcp
That is good to get started. Then start with a small tutorial, this is a simple 2D space shooter : http://www.unityjumpstart.com/ProofOfConcept_1/ : click on the videos part1.mp4 part2,3,4 =]
I found another by Eric : http://wiki.unity3d.com/index.php?title=2DShooter : http://forum.unity3d.com/threads/7883-2D-shooter-tutorial
By then you should be getting the hang of things and starting to have ideas of your own. When you decide what kind of game you want to make, then look at each part you'll need. For example, if you want to make some terrain then walk around it with a character : http://cgcookie.com/unity/2011/12/05/introduction-to-character-controllers/
Basically then just search for tutorials, there are many out there, either written or on youtube.
the Unity Wiki tutorials : http://wiki.unity3d.com/index.php/Tutorials
A big list of tutorials : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html
A very helpful 'site, all in C# : http://unitygems.com/
Helpful page with information on using Built-In Arrays and Lists (you'll need this later!) : http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?
The unity wiki link above is very handy with lots of scripts and shaders too (just check out all the links down the left, and the tabs along the top : http://wiki.unity3d.com/index.php/Scripts )
http://answers.unity3d.com/questions/148211/list-of-frequently-asked-beginners-questions.html
http://forum.unity3d.com/threads/132628-How-to-help-the-Absolute-Beginner
Wow, really? So if boolean is a boolean, (as it should be) what's the point of System.Boolean, and who is responsible for cruelly tricking me into typing 8 more characters than I needed to for all this time?
I really don't know dude, I've never seen your method before. Let the headhunting begin for whoever told you that =]
Answer by infinitypbr · Jun 23, 2013 at 02:19 AM
function Start ()
{
if (Dalekgun != null)
{
hasDalek = true;
}
}
If I'm not mistaken, your code is trying to say, "If the GameObject 'Dalekgun' exists, then 'nasDalek' is True"? And then repeat for the rest.
Your answer
Follow this Question
Related Questions
Easy collision script not working, don't understand. 2 Answers
Making opening door requires a Key 2 Answers
Cannot touch cloned prefabs 0 Answers
Basketball Game: Need Help! 2 Answers
Programming noob help 1 Answer