- Home /
Multiple istance of the same script
Hi! I'm trying to create a 2d platform scene and i need to place a script that animates multiple copies of the same gameObject. The problems that the script attached to it makes animating all gameObject with this script. How can i separate the behavior of every singular gameObject? Here's the script
using UnityEngine;
using System.Collections;
class GrassAnimationScript : MonoBehaviour
{
private bool onGrass;
public float grassRadius = 1;
public Transform groundCheck;
public LayerMask Grasses;
public Sprite sprite0;
public Sprite sprite1;
public float timer = 1;
public float timerSpeed= 1;
private bool wichSprite;
private SpriteRenderer myRenderer;
// Use this for initialization
void Start ()
{
myRenderer = gameObject.GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
onGrass = Physics2D.OverlapCircle (groundCheck.position, grassRadius, Grasses);
if(onGrass == true && Input.GetAxis("Horizontal") != 0)
{
if (wichSprite == true)
{
myRenderer.sprite = sprite0;
timer -= Time.deltaTime*timerSpeed;
if (timer <= 0)
wichSprite = false;
}
else if (wichSprite == false)
{
myRenderer.sprite= sprite1;
timer += Time.deltaTime*timerSpeed;
if (timer >= 1)
wichSprite= true;
}
}
else
{
myRenderer.sprite = sprite0;
timer = 1;
}
}
}
Same result using simple animator controller.
Answer by borro56 · Apr 04, 2014 at 05:22 PM
Are you sure GroundCheck is not the same for everybody? Maybe you accidentally setted the same for every instance, and as far as i undestand what you are wanting to do, that property should be different for each instance, i assume is some sort of dummy in the foots of the players.
Are you controlling various objects at the same time?, because the Input.GetAxis will move everybody.
Answer by koray1396 · Apr 04, 2014 at 03:56 PM
you define a public bool, let's say isAnimating. check the ones on editor that will animate.
and put the following.
public bool isAnimating;
then
if(isAnimating){
//animation method
}
Only the ones that has isAnimating activated on inspector will animate, but if i activate two gameobjects with the script attached they will animate as they share variables, methods etc. so they animate at the same time despite that one satisfies my method conditions and the other one doesn't.
no, you can check isAnimating on one object and not on the other. so one of them will animate, the other will not. they will share variables and methods but not the values of the variables. you can assign different values for different objects on the inspector.
Yes but so onGrass check should be different for each object, but if it turns true on one of them it turns true over all other objects
Answer by Ermarrero · Apr 04, 2014 at 04:10 PM
try something like this, just add the gameobjects you want to animate on the childObjects variable, and namespace for Collections.Generic. . Let me know if it works.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GrassAnimationScript : MonoBehaviour {
public List<GameObject> childObjects;
public float grassRadius = 1;
public Transform groundCheck;
public LayerMask Grasses;
public Sprite sprite0;
public Sprite sprite1;
public float timer = 1;
public float timerSpeed = 1;
private bool wichSprite;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
foreach (GameObject go in childObjects)
{
SpriteRenderer myRenderer = go.GetComponent<SpriteRenderer>();
bool onGrass = Physics2D.OverlapCircle(groundCheck.position, grassRadius, Grasses);
if (onGrass == true && Input.GetAxis("Horizontal") != 0)
{
if (wichSprite == true)
{
myRenderer.sprite = sprite0;
timer -= Time.deltaTime * timerSpeed;
if (timer <= 0)
wichSprite = false;
}
else if (wichSprite == false)
{
myRenderer.sprite = sprite1;
timer += Time.deltaTime * timerSpeed;
if (timer >= 1)
wichSprite = true;
}
}
else
{
myRenderer.sprite = sprite0;
timer = 1;
}
}
}
}