• 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 /
This question was closed Sep 25, 2020 at 02:18 PM by twotostudio for the following reason:

Other

avatar image
0
Question by twotostudio · Sep 24, 2020 at 06:20 PM · arrayscore

score system according to the object added to the array

 public class move : MonoBehaviour
 {
   public GameObject a;
   public GameObject b;
   public GameObject c;
   public GameObject lefthand;
   public GameObject righthand;
   public Transform[] lchilds;
   public Transform[] rchilds;
  int score;
     
     private void Update()
   {             
         lchilds = new Transform[lefthand.transform.childCount];
         for (int i = 0; i < lefthand.transform.childCount; i++)
         {
             lchilds[i] = lefthand.transform.GetChild(i);           
         }
     
         rchilds = new Transform[righthand.transform.childCount]
         for (int i = 0; i < rcount; i++)
         {
             rchilds[i] = righthand.transform.GetChild(i);
         }
    }
       

I'm making two arrays. Objects are added to these during the game. Let these be objects a, b, c. If a is added to the lists, there should be 10 scores, 20 scores if b is added, and 30 score increments if c is added. I could not find how to define this condition. How can I collect scores based on the object added with a conditional statement?

My method of adding objects to arrays is the same as in the load script.

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

  • Sort: 
avatar image
1

Answer by Chrissyeah_ · Sep 24, 2020 at 07:43 PM

I'm a bit confused by how you are defining a b and c, if it's simply to check if its the same object then there.

 lchilds = new Transform[lefthand.transform.childCount];
         for (int i = 0; i < lefthand.transform.childCount; i++)
         {
             lchilds[i] = lefthand.transform.GetChild(i);
             if (lchilds[i].gameObject == a)
                 score += 10;
             if (lchilds[i].gameObject == b)
                 score += 20;
             if (lchilds[i].gameObject == c)
                 score += 30;
         }

If that fails and you want to isolate some ID from the object you can add a script called ID with a string id.

And say if (lchilds[i].GetComponent<ID>().id == "A")

Comment
Add comment · Show 11 · 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 twotostudio · Sep 24, 2020 at 08:29 PM 0
Share

thank u so much

avatar image twotostudio · Sep 24, 2020 at 08:44 PM 0
Share

Objects a, b, c are prefabs. And they're not on stage. Their clones are added as children. How should I define it?

avatar image rh_galaxy twotostudio · Sep 24, 2020 at 09:16 PM 0
Share

When you create your clones, you could set a name to the objects so you can identify them later.

 GameObject o = Instantiate(a, ...);
 o.name = "A";
 o = Instantiate(b, ...);
 o.name = "B";

And

 if (lchilds[i].name == "A")
     score += 10;
avatar image twotostudio rh_galaxy · Sep 24, 2020 at 10:32 PM 0
Share
 public class spawnobjects : $$anonymous$$onoBehaviour
 {
     public GameObject[] myobjects;
     public Transform [] spawnPos;
     int rando$$anonymous$$t;
     public bool stopspawn = false;
     public float spawntime;
     public float spawndelay;
     public static int skor;
     T$$anonymous$$Pro.Text$$anonymous$$eshProUGUI skor_txt;
    
     void Start()
     {
         GameObject o = Instantiate(myobjects[0]);
         o.name = "A";
         o = Instantiate(myobjects[1]);
         o.name = "B";
         o = Instantiate(myobjects[2]);
         o.name = "C";
         skor = 0;
         InvokeRepeating("SpawnRandom", spawntime, spawndelay);
         skor_txt = GameObject.Find("Canvas/skor_txt").GetComponent<Text$$anonymous$$eshProUGUI>();
     }
 
     void SpawnRandom()
     {
         int spawn = UnityEngine.Random.Range(0, spawnPos.Length);
         int objects = UnityEngine.Random.Range(0, myobjects.Length);
         Instantiate(myobjects[objects], spawnPos[spawn].transform.position, Quaternion.identity);
     }
 
   
 
     void Update()
     {
        
         skor_txt.text = skor.ToString();
         if (stopspawn)
         {
             CancelInvoke("SpawnRandom");
         }
     }
 }


This is the code for spawned objects. I tried doing what you said here but I guess I did it wrong. And the script for making child is a separate cs file.

Show more comments

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

How to put Streamwriter into an array? 1 Answer

help with score display concept 2 Answers

Sorting an array - C# 3 Answers

Deactivated Targets in Shooting Game Cause Score to Never Stop Adding One 1 Answer

Is there a decent tutorial for a local high score table (android)? 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