- Home /
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);
}
}
I want to make the value for fallSpeed and spinSpeed to = 0.
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.
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.
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 !
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.
It worked, but could I make it do cloned objects? For example watermelon(Clone).
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220611233525im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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