Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sg155 · Jun 23, 2013 at 01:47 AM · destroyobjectsboolean

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;
    }
    
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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;

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image infinitypbr · Jun 23, 2013 at 02:16 AM 0
Share
 var hasCasing : boolean = false;

That is proper for setting boolean variables.

avatar image AlucardJay · Jun 23, 2013 at 02:21 AM 0
Share

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

avatar image Kiloblargh · Jun 23, 2013 at 02:25 AM 0
Share

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?

avatar image AlucardJay · Jun 23, 2013 at 02:28 AM 0
Share

I really don't know dude, I've never seen your method before. Let the headhunting begin for whoever told you that =]

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges