Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Mempsis · Aug 24, 2015 at 11:29 AM · scripting problemelseif else

Making a gameobject inactive and locating a clone. What's wrong with the script?

So I'm kind of a newbie with programming and C#, but for a schoolwork I'm trying to program a shooter game. I have a problem with my code. I'm trying to make the player inactive if it crushes to an enemy, and locate a clone on the prefab's original spot. After three times it should be game over. However, I've already used many hours to figure out how to do that and I'm getting very frustrated.

 public Transform player;
 public int lives = 3;

 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Enemy") 
     {
         lives--;
         
         if (lives > 0);
         {
             gameObject.SetActive(false);
             Instantiate(player, new Vector3(3f, 6f, -17f), Quaternion.identity));
         }
         
         else 
         {
             gameObject.Destroy(player);
             Application.LoadLevel("GameOverScene");
         }
     }
 }


This script gives me an 'Unexpected symbol' and the else here is underlined with red. Can't you use else inside of another if or what is the problem?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Scribe · Aug 24, 2015 at 11:37 AM

The error is because you have a semicolon ';' after the second if (line 10) so you never enter your if, and so you never have anything to match the else to.

The OnTriggerEnter is only called however, if this object the script is attached to (gameObject) collides with enemy, presumably therefore, you are instantiating the same object again, but if that is the case, why not just reset this ones position instead?

Comment
Add comment · Show 2 · 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 Mempsis · Aug 24, 2015 at 02:41 PM 1
Share

I did the changes and edited it to reset the position. Now it works like a charm, thank you! :)

avatar image Landern · Aug 24, 2015 at 02:45 PM 0
Share

@$$anonymous$$empsis, you should mark @Scribe's answer as correct.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to check if the difference between any pair of numbers in an array is less than some value? 0 Answers

If statement seeming to give false positive 2 Answers

How to Instantiate prefab when dragging an object through a specific area? 1 Answer

Teleporting to outside of constraits 0 Answers

Unity freezing on Waitforsecond 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