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 /
  • Help Room /
This question was closed May 28, 2017 at 06:24 PM by codytremblay for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by codytremblay · May 28, 2017 at 08:20 AM · scripting problemvariables

Why ain't my Code working?

I trying to have a system that spawns enemies and then wait till all dead to spawn more. When an enemy dies it suppose to lower enemyalive; No matter how the enemy dies I want it to drop enemyalive by 1 and if it hits player in collision that just game over & got that handled.

here is health script:

 public float health;
 public Controler Con; // here i tried linking it to the Controler script

 void OnTriggerEnter2D(){
     health -= 1;
 }
 void Update () {
     if (health <= 0) {
         Die ();
     }
 }
 void Die(){
     if (gameObject.tag == "Enemy") {
         Con.enemyalive -= 1; // here it suppose to access the script Controler and minus the number by 1
     }
     Destroy (gameObject);
 }

}

if anyone can fix this or know better way to check if none exists please say i already tried one way but it only works if not null;

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

  • Sort: 
avatar image
0
Best Answer

Answer by codytremblay · May 28, 2017 at 04:20 PM

after several hours of yelling. I just made a script just for enemy.

here the code

 public int health;
 public Controler Con;
 
 void Start(){
     Con = GameObject.Find("Player").GetComponent<Controler>(); // I was missing this part ty oscarklm for giving me it.
 }
 
 void OnTriggerEnter2D(){
     health -= 1;
 }
 void Update () {
     if (health < 1){
         Die();
     }
 }
 void Die(){
     Con.enemyAlive -= 1; // here it takes 1 away so i see who is alive or dead
     Destroy(gameObject);
 }
Comment
Add comment · Show 1 · 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 oscarklm · May 28, 2017 at 04:24 PM 0
Share

Glad you got what i meant with my example. Happy coding!

avatar image
1

Answer by oscarklm · May 28, 2017 at 11:20 AM

Hey!

Looks like you're making this a bit confusing for yourself. I've attached a way of doing this for you. Hopefully that will help you achieve what your trying to do.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class HealthScript : MonoBehaviour {
     public int health; // You are better off using integer for your health, floats are mostly used for more precise measurements
     public Controller con;
 
     // Use this for initialization
     void Start () {
 
         // We need to find a player with the Controller attached and set con to that controller, to be able to use it properly
         con = GameObject.Find("Player").GetComponent<Controller>();
     }
     
     // Update is called once per frame
     void Update () {
 
         if (health <= 0)
         {
             Die();
         }
         
     }
 
     void Die()
     {
         Destroy(gameObject);
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == "Enemy")
         {
             con.enemiesAlive -= 1; // Subtracting 1 from enemiesAlive
         }
     }
 }
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 codytremblay · May 28, 2017 at 02:21 PM 0
Share

this script is on many things. If I did your way no one could lose health it would only end in GameOver so no use for levels.

avatar image oscarklm codytremblay · May 28, 2017 at 04:24 PM 0
Share

I wasn't telling you how to code what you're trying to do. Only explaining you the importance of setting the con to the component of Controller attached to a GameObject :)

// We need to find a player with the Controller attached and set con to that controller, to be able to use it properly con = GameObject.Find("Player").GetComponent();

Follow this Question

Answers Answers and Comments

139 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 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Calling variables from other script 1 Answer

Unity 5 Accessing other scripts problem 2 Answers

Add a script to a GameObject via script at runtime? 1 Answer

I want to access a variable in another object's script whose GameObject is stored in a variable in script 1. 1 Answer

Game Objects not using same script independently. 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