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 BrighterWorld · Feb 09, 2020 at 05:31 AM · singletonobject referencenonstaticmultiple instaces

I can't use a public non-static int in another script

I have this enemy and it has 2 scripts. One tells if it's been hit and the other one basically does something if it has been hit. Now, I need to use an integer for the enemy's health "wasphealth" in the Hit script but it only lets me do it if it's static. I can't really make it static because I'll be duplicating this enemy. SO I need to implement a Singleton, right? Yeah, well for the life of me I couldn't figure out how to do it... Do you guys have any suggestions? Hit: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class hit : MonoBehaviour {
 
     
     public Rigidbody expl;
 
     void Start () {
         
     }
     void Update () {
     
     }
 
     void OnTriggerEnter (Collider other)
     {
         if (other.tag == "bullet") {
             colliderinstantiator.wasphealth += 1;
             Debug.Log (colliderinstantiator.wasphealth);
 
             Instantiate(expl, GameObject.FindWithTag("bullet").transform.position, Quaternion.identity);
             Destroy (GameObject.FindWithTag ("bullet"));
         }
     }
 }

The other script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class colliderinstantiator : MonoBehaviour {
 
     public Rigidbody wasp;
     public Rigidbody pigeon;
     public Rigidbody bullet;
     public int wasphealth = 0; //this is the problem
     public float minDis = 10.0f;
     public float drag = 30.0f;
     public ParticleSystem smoke;
     public ParticleSystem broke;
     public Rigidbody eexpl;
     public bool smokerb = false;
     public bool brokerb = false;
 
     void Start () 
     {
         
     }
     void Update ()
     {
         if (wasp) {    
             if (wasp.transform.position.z - pigeon.transform.position.z < 200) {
                 GetComponent<chabos>().enabled = false;
             }
             
 
             if (wasphealth >= 10 && wasphealth <= 20)
             {
                 if (smokerb == false)
                 {
                     Invoke("Smoker", 0f);
 
                 }
             }
             else if (wasphealth > 20) { Destroy(GameObject.FindWithTag("smoke1")); }
             
             if (wasphealth >= 21 && wasphealth <= 29) 
             {
                 if (brokerb == false)
                 {
                     Invoke("Broker", 0f);
                 }
             
             }
             else if (wasphealth > 29) { Destroy(GameObject.FindWithTag("broke1")); }
             if (wasphealth >= 30) {
                 Destroy (GameObject.FindWithTag ("nmy1"), 0.1f);
                 
             }
             if (wasphealth == 30) {
                 Instantiate(eexpl, GameObject.FindWithTag("nmy1").transform.position, Quaternion.identity);
 
             }
 
         }
     }
     void Smoker ()
     {
         Instantiate (smoke, GameObject.FindWithTag ("nmy1").transform.position, Quaternion.identity);
         smokerb = true;
     }
     void Broker ()
     {
         Instantiate (broke, GameObject.FindWithTag ("nmy1").transform.position, Quaternion.identity);
         brokerb = true;
     }
 }
 
 

NOTE: you don't have to look at the whole script, just the int part

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
0

Answer by Namey5 · Feb 09, 2020 at 08:39 AM

Implementing a singleton would just shift the problem in this case. You would end up only being able to have one enemy in the scene, which kinda defeats the purpose of all the OOP stuff you're doing in these scripts. Instead, all you have to do is access the instance of the class, rather than the class itself;

 void OnTriggerEnter (Collider other)
 {
     colliderinstantiator instance = GetComponent<colliderinstantiator>();
     if (other.tag == "bullet") {
         instance.wasphealth += 1;
         Debug.Log (instance.wasphealth);
  
         Instantiate(expl, GameObject.FindWithTag("bullet").transform.position, Quaternion.identity);
         Destroy (GameObject.FindWithTag ("bullet"));
     }
 }

A few things to note however;

  • There's no reason these need to be separate scripts, just move your 'hit' code inside the main script

  • Not necessary, but it's helpful to properly capitalise your class names to distinguish them from their instances

  • 'Invoke ("", 0f)' is essentially the same as just calling the function, so it would be more beneficial to just do that, i.e. 'Smoker()'

  • 'FindWithTag()' is a surprisingly expensive function, so if you can't just assign the GameObject some other way, it would be better to only call it once and cache the result Hope that helps.

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

119 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

Related Questions

Static List< GameObject> in Singleton 1 Answer

singleton becomes null? 0 Answers

Objects of global singleton aren't accessible 1 Answer

Is there a known issue where singletons will run constructors multiple times? 0 Answers

StartCoroutine by another class (Coroutine inside Coroutine - Instance class) 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