Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 RaffaLinux · Apr 04, 2014 at 03:25 PM · animationgameobject

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.

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

3 Replies

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

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.

Comment
Add comment · 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
0

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
 }
Comment
Add comment · Show 3 · 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 RaffaLinux · Apr 04, 2014 at 04:21 PM 0
Share

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.

avatar image koray1396 · Apr 04, 2014 at 04:43 PM 0
Share

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.

avatar image RaffaLinux · Apr 04, 2014 at 05:57 PM 0
Share

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

avatar image
0

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;
            }
         }
 
         }
 
         
 }
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 RaffaLinux · Apr 04, 2014 at 04:17 PM 0
Share

Thank you anyway, but it didn't worked. :/

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

23 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

Related Questions

How could it be done "Cooldown" for a shield ? 2 Answers

why not run animation when doublication gameObject ???? 0 Answers

Animation not playing via collider and script (game just chugs instead) 2 Answers

GameObject reacts to audio source 1 Answer

C# Make Child Not Animate Looped Animation 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