Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Dollar-Fish · Jul 29, 2015 at 11:44 AM · objectunity 2dgetcomponentinteger

Change Integer to All Gameobjects With Script

I want to change an integer value to all game objects that hold the script. So that mean I want to update that number to every gameobject with that script, I want it to get the components at the beginning of the script, and then change the number when the thing is on collision. The way the script is now doesn't really work because you are getting the component when on collision. It works sometimes but not all the time because the system can't go through all of them instantly.

Collision Script

 using UnityEngine;
 using System.Collections;
 
 public class senseiCollision : MonoBehaviour {
 
     public GameObject watermelon;
     public GameObject strawberry;
     public GameObject grapes;
     public GameObject banana;
     public GameObject orange;
     public GameObject tomato;
     public GameObject pineapple;
     public GameObject apple;
     public GameObject blueberry;
     public GameObject raspberries;
     public GameObject pear;
     public GameObject blackberries;
     public GameObject lemon;
     private fruitFall fruitFall;
 
     private Animator anim;
 
     private fruitFall fruitfall;
 
     private spawnFruit spawnfruit;
 
     void Awake()
     {
         anim = gameObject.GetComponent<Animator>();
         spawnfruit = GameObject.Find("spawnFruit").GetComponent<spawnFruit>();
         GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
         GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
         GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
         GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         GameObject.Find("banana(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
         GameObject.Find("banana(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         GameObject.Find("orange(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
         GameObject.Find("orange(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
         GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
         GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         GameObject.Find("apple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
         GameObject.Find("apple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
         GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
         GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         GameObject.Find("pear(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
         GameObject.Find("pear(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
         GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
         GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
     }
 
     void OnTriggerEnter2D(Collider2D col)
     {
         if(col.CompareTag("watermelon"))
         {
             anim.SetBool("Awake", true);
             spawnfruit.fruitHit = true;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         }
         if(col.CompareTag("strawberry"))
         {
             anim.SetBool("Awake", true);
             spawnfruit.fruitHit = true;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         }
         if(col.CompareTag("grapes"))
         {
             anim.SetBool("Awake", true);
             spawnfruit.fruitHit = true;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         }
         if(col.CompareTag("banana"))
         {
             anim.SetBool("Awake", true);
             spawnfruit.fruitHit = true;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         }
         if(col.CompareTag("orange"))
         {
             anim.SetBool("Awake", true);
             spawnfruit.fruitHit = true;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         }
         if(col.CompareTag("tomato"))
         {
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         }
         if(col.CompareTag("pineapple"))
         {
             anim.SetBool("Awake", true);
             spawnfruit.fruitHit = true;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         }
         if(col.CompareTag("apple"))
         {
             anim.SetBool("Awake", true);
             spawnfruit.fruitHit = true;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         }
         if(col.CompareTag("blueberry"))
         {
             anim.SetBool("Awake", true);
             spawnfruit.fruitHit = true;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         }
         if(col.CompareTag("raspberries"))
         {
             anim.SetBool("Awake", true);
             spawnfruit.fruitHit = true;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         }
         if(col.CompareTag("pear"))
         {
             anim.SetBool("Awake", true);
             spawnfruit.fruitHit = true;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         }
         if(col.CompareTag("blackberries"))
         {
             anim.SetBool("Awake", true);
             spawnfruit.fruitHit = true;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         }
         if(col.CompareTag("lemon"))
         {
             anim.SetBool("Awake", true);
             spawnfruit.fruitHit = true;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("lemon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("watermelon(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("strawberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("grapes(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("banana(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("orange(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("tomato(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pineapple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("apple(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blueberry(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("raspberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("pear(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().fallSpeed = 0;
             GameObject.Find("blackberries(Clone)").GetComponent<fruitFall>().spinSpeed = 0;
         }
 
     }
 }

Fruit Fall Script(probably won't need this, but just in case)

 using UnityEngine;
 using System.Collections;
 
 public class fruitFall : MonoBehaviour {
     
     public float fallSpeed = 3f;
     public float spinSpeed = -45f;
     
     void Update() {
         
         transform.Translate(Vector2.down * fallSpeed * Time.deltaTime, Space.World);
         transform.Rotate(Vector3.forward, spinSpeed * Time.deltaTime);
         
     }
     
 }



Comment
Add comment · Show 3
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 Dollar-Fish · Jul 29, 2015 at 04:23 AM 0
Share

I want to make the value for fallSpeed and spinSpeed to = 0.

avatar image Dollar-Fish · Jul 29, 2015 at 04:24 AM 0
Share

I know I don't need all the text, I just want to make it less lines of code and fast for when it needs to call it.

avatar image _Gkxd · Jul 30, 2015 at 05:56 PM 0
Share

Why not put the collision handling in the fruit script?

Every fruit would have a fruit script, and each fruit would handle its own collision.

It's not very clear what you're trying to do with this code, so I might be misunderstanding something.

1 Reply

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

Answer by Hellium · Jul 29, 2015 at 07:51 AM

The following function is what you need :

Object.FindObjectsOfType

It returns an array containing all the game objects holding a specific script (here fruitFall)

http://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html


Here is an example :

 private fruitFall[] fruitFalls ;
 
 void Awake()
 {
     fruitFalls = Object.FindObjectsOfType(typeof(fruitFall)) as fruitFall[];
     ...
 }
 
 void SetFallSpeed ( int newValue )
 {
     for( int fruitIndex = 0 ; fruitIndex < fruitFalls.Length ; ++fruitIndex)
     {
         fruitFalls[fruitIndex].fallSpeed  = newValue ;
     }
 }
 
 void SetSpinSpeed ( int newValue )
 {
     for( int fruitIndex = 0 ; fruitIndex < fruitFalls.Length ; ++fruitIndex)
     {
         fruitFalls[fruitIndex].spinSpeed  = newValue ;
     }
 }

You could change the for loop for a foreach loop if you prefere. Moreover, you can merge the SetFallSpeed and SetSpinSpeed if you want too.

Then in your function, instead of setting the int values individually, just call SetFallSpeed or SetSpinSpeed with the desired value. But be carefull ! Every object holding the fruitFall script will have its fallSpeed / spinSpeed value changed !

Comment
Add comment · Show 9 · 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 Dollar-Fish · Jul 30, 2015 at 04:56 PM 0
Share

Could you give me an example? I don't understand.

avatar image Hellium · Jul 30, 2015 at 05:03 PM 0
Share

@Dollar Fish : Answer edited

avatar image TheHeron · Jul 30, 2015 at 05:49 PM 0
Share

This is definitely one option for how you can handle this problem.

Another possibility for what you could do is, assu$$anonymous$$g you /always/ want all fruit to always share the same speed, you could declare a static int or float or whatever and use it between all instances. https://msdn.microsoft.com/en-us/library/98f28cdx.aspx might help you handle that.

avatar image Dollar-Fish · Jul 30, 2015 at 07:31 PM 0
Share

It worked, but could I make it do cloned objects? For example watermelon(Clone).

avatar image Hellium · Jul 30, 2015 at 07:57 PM 0
Share

I didn't understand your question.

The functions I proposed will change the integer values of all the gameObjects with a fruitFall script, even the cloned objects present in the scene.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can i get a Transform component from an other object's script? 1 Answer

How to cut an object in any direction 2 Answers

How do I change Sprites (and manipulate the Sprite Object) in C#? 0 Answers

How to instantiate prefab when colliding with specified prefab 1 Answer

NullReferenceException: Object reference not set to an instance of an object ..... 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