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 DCutting · Feb 13, 2015 at 06:57 AM · prefabunity 2d

Prefab Objects all reacting to the collision of one

still pretty new to unity so I might be missing something simple here but... I am making a simple platformer and on a few of my levels I am putting in some enemy's(from a prefab) which will kill the player on contact and on contact with the world will bounce and go the other way. Only for some reason they all seem to react as if they've hit the world at the exact same time, even if only one of them has hit something, resulting in enemy's moving back and forward without hitting anything because the enemy's on either ends of the line are hitting. My code is below and I just cant understand why this is happening. As said probably just missing something simple but would appreciate any help.

 using UnityEngine;
 using System.Collections;
 
 public class EnemyBasic : MonoBehaviour {
     public static float maxspeed = 5f;
     public bool facingright = true;
     public AudioClip[] audioClip;
 
     // Use this for initialization
     void Start () 
     {
         
     }
     
     // Update is called once per frame
     void Update () 
     {
         rigidbody2D.velocity = new Vector2 (maxspeed, rigidbody2D.velocity.y);
     }
     void OnTriggerEnter2D(Collider2D other)
     {
         if(other.gameObject.tag == "World")
         {
             Debug.Log ("World hit detected");
             maxspeed *= -1;
         }
         else if(other.gameObject.tag == "Player")
         {
             Debug.Log ("player hit detected");
             EnemyPlaySound (0);
             Destroy(other.gameObject);
             Application.LoadLevel(Application.loadedLevelName);
         }
     }
     void Flip()
     {
         facingright = !facingright;
         Vector2 thescale = transform.localScale;
         thescale.x *= -1;
         transform.localScale = thescale;
     }
     void EnemyPlaySound(int clip)
     {
         audio.clip = audioClip [clip];
         audio.Play ();
     }
Comment
Add comment · Show 2
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 DCutting · Feb 13, 2015 at 07:30 AM 0
Share

Ah, as said still pretty new to C so was wondering what exactly the static word did, however is there a way to fix the problem without changing the word static, or else another way to allow the variable to be accessed from another script

avatar image tanoshimi · Feb 13, 2015 at 10:40 AM 0
Share

You access components on other objects using GetComponent(), not static. Learn how to use it - you'll need it a lot :) http://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Feb 13, 2015 at 06:59 AM

Because you've declared maxspeed as static, set by the class rather than an instance of the class.

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

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

2 People are following this question.

avatar image avatar image

Related Questions

Changing transform.localScale of a prefab in the script not working. Help pls! 1 Answer

What is the correct way to use prefab in my example? 2 Answers

How to instantiate prefab when colliding with specified prefab 1 Answer

Box Collider 2d don't stop the object (prefab) 2 Answers

Unexpected symbol 'Instantiate' [SOLVED] 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