Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Rizizum · Jan 03, 2020 at 04:31 PM · instantiateclones

How to change the variables from an specific clone script?

I'm trying to make a cell game, in it the cells will gather food which will increase their fullness and once they reach a certain level of fullness they replicate, the thing is that when the cells take the food, the fullness increases only in the last cell instantiated instead of in the cell that touched the food

Script in the food:

 void OnTriggerEnter2D(Collider2D collisor)
     {
         if (collisor.tag == "Cell")
         {
             cellFoodLogic.instace.fullness += 10;
             Debug.Log("Fullness: " + cellFoodLogic.instace.fullness);
             Destroy(this.gameObject);
         }
     }

script on cells:

 public class cellFoodLogic : MonoBehaviour
 {
     public static cellFoodLogic instace;
     public GameObject daughterCell1;
     public GameObject daughterCell2;
     public int fullness;
     public float fullnessDecreaseSpeed;
     private float fullnessDecreaseTimer;
     Transform tr;
     // Start is called before the first frame update
     void Start()
     {
         instace = this;
         tr = GetComponent<Transform>();
         fullness = 50;
         fullnessDecreaseTimer = fullnessDecreaseSpeed;
 
     }
 
     // Update is called once per frame
     void Update()
     {
         fullnessDecreaseTimer -= Time.deltaTime;
         if(fullnessDecreaseTimer<=0)
         {
             fullness -= 10;
             fullnessDecreaseTimer = fullnessDecreaseSpeed;
             Debug.Log("Fullness: "+fullness);
         }
         if (fullness<=0)
         {
             Destroy(this.gameObject);
         }
         if(fullness>=100)
         {
             fullness = 50;
             Instantiate(daughterCell1, tr.position, Quaternion.identity);
 
         }
     }        
 }
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
2
Best Answer

Answer by unity_ek98vnTRplGj8Q · Jan 03, 2020 at 04:47 PM

The issue is that you are using a STATIC variable to keep track of your cells. public static cellFoodLogic instace;This is essentially what is known as a singleton: it is useful if you only expect 1 instance of a class to ever exist. You, however, will have many cell instances, and you want the collision trigger to find the associated cell instance. This is not possible using the static variable in the way that you are. Every time you call cellFoodLogic.instance it will always point to the same cell, no matter where you call it from.


To fix this, you will need to find a way to get the specific instance associated with the cell. The easiest way to do this is to use GetComponent from the collision script.

Try this:

 void OnTriggerEnter2D(Collider2D collisor)
      {
          if (collisor.tag == "Cell")
          {        
              //cellFoodLogic.instace.fullness += 10;  <------Faulty line of code
              
              cellFoodLogic cell = collisor.gameObject.GetComponent<cellFoodLogic>();  
              if(cell!=null){
                 cell.fullness += 10;
                 Debug.Log("Fullness: " + cell.fullness);
              }
              
              Destroy(this.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 Biticalifi · Feb 22 at 07:52 AM 0
Share

I have similar problem do you think you could try to help?

I am trying to make a game where multiple enemies come down to attack. I Instantiate them and have them randomly come down. But when I check if they are touching a bullet, only the last instantiated enemy can check it. Here is the link to original post with the code: https://answers.unity.com/questions/1888391/how-do-i-check-the-variable-of-a-specific-clone.html?sort=votes

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

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

Related Questions

Prefab issue - all appear at same location 2 Answers

instantiated gameobjects(clone) don't behave like its original gameobject. 1 Answer

My script only makes two clones. 3 Answers

restricting clone number 2 Answers

All subsequent instantiated objects = auto-named with suffix (Clone)? 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