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 reineinmyheart · Nov 25, 2013 at 09:45 AM · prefab

Multiple prefabs in game scene

We are creating a simple AI for a platform game, wherein a number of enemies are patrolling a certain platform. They will change direction if they reach the edge of that platform. Since they are all going to do the same kind of behavior, we just made a prefab of that enemy, with a script attached to it.

So we duplicated them and placed them in different places, and just changed the values of the left and right limits in the inspector.

However, they are all changing directions at the same time, even if one enemy hasn't reached the limit yet.

Here's the script:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyPatrol : MonoBehaviour {
 
     public float RightSpeed = 2.0f;
     public float Leftspeed =- 2.0f;
 
     public Animator anim;
     public string axisName = "Horizontal";
     public float speed = 1.0f;
 
     public float RightEdge;
     public float LeftEdge;
     
     public static bool isRight = true;
     private Transform myTransform;
 
     // Use this for initialization
     void Start () {
         anim = gameObject.GetComponent<Animator>();
         myTransform = transform;
 
         int rand = Random.Range(1, 2);
         if(rand == 1) isRight = true;
         else isRight = false;
     }
     
     // Update is called once per frame
     void Update () {
 
         anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis(axisName)));
 
         if (isRight) {
             Vector3 newScale = myTransform.localScale;
             newScale.x = 1.0f;
             myTransform.localScale = newScale;
             myTransform.position += myTransform.right * RightSpeed *Time.deltaTime;
         }
         else if (!isRight) {
             Vector3 newScale = myTransform.localScale;
             newScale.x = -1.0f;
             myTransform.localScale = newScale;
             myTransform.position += myTransform.right * Leftspeed *Time.deltaTime;
         }
 
         if(myTransform.position.x > RightEdge){
             isRight = false;
         }
         else if(myTransform.position.x < LeftEdge){
             isRight = true;
         }
         myTransform.position += myTransform.right * Input.GetAxis(axisName) * speed * Time.deltaTime;
 
         
 
      }
 }


What seems to be the problem? :( Thanks for your help.

Comment
Add comment · Show 1
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 raimon.massanet · Nov 25, 2013 at 10:27 AM 0
Share

Why are you taking into account the input axis when moving enemies?

1 Reply

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

Answer by yatagarasu · Nov 25, 2013 at 10:58 AM

public static bool isRight = true; should not be static. imho

Comment
Add comment · Show 2 · 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 raimon.massanet · Nov 25, 2013 at 01:14 PM 0
Share

I agree. That should fix the problem.

avatar image thef1chesser · Nov 25, 2013 at 01:44 PM 0
Share

making that bool static makes certain there is only one bool isRight. By changing is in any script will change the one bool isRight, where OP just wants to change this bool for one enemy.

So remove the static and create getters and setters if you need to change the bool in other classes.

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

19 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I render a prefab without any scripts/components running? 3 Answers

Show and Hide a prefab or GameObject 10 Answers

cloned game object wont call script right... 2 Answers

How to find a prefab in another scene? 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