• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by nikocr33nzz · Nov 25, 2016 at 07:58 AM · scripting problemscripting beginnerscriptingbasicsscript error

weird problem with scripting

so i have prefab called "enemy" and in that enemy have a script named "enemyBlue_script". and also i have 1 another script called rulebase and it attached to gameobject " game_controller" . in that rulebase script i have some method , one of them is fire() method. and i tried to call it in the enemyBlue_script in the update() function. when i run the game the enemy can firing but the other enemy can't. when the enemy which can fire has destroyed then the other enemy can fire.

this is my script

rulebase script

 using UnityEngine;
 using System.Collections;
 
 public class Rulebase : MonoBehaviour {
 public float fireRate = 5F;
 public GameObject shot;    
 private float nextFire = 0.0F;        
 
 
 public void Fire(Transform shotSpawn){
 
 if (Time.time > nextFire) {        
         nextFire = Time.time +fireRate;                             //Increment nextFire time with the current system time + fireRate
         Instantiate (shot, shotSpawn.position, shotSpawn.rotation);         //Instantiate fire shot
 } 
 
 }
 
 }


and this is enemyBlue_script which called the fire function from the rulebase script

 using UnityEngine;
 using System.Collections;
 
 public class EnemyBlue_Script : MonoBehaviour 
 {
 
 public Transform shotSpawn;
 
 
 void Update(){
 Rules.Fire (shotSpawn);
 }
 


the result of that script is something like this

alt text

as you can see the other enemy wont fire. thanks for the help

ask4.jpg (16.6 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Nov 25, 2016 at 10:41 AM

Well, of course when you only have one "Rulebase" instance in your scene and every enemy is using the same script, you only have one "nextFire" timeout variable. All enemies are calling the Fire method but the timeout prevents that anything happens.

Once the timout is reached the first enemy that calls Fire this frame will actually shoot one bullet. However it also updates the nextFire variable so the next enemy is blocked again.

Why don't you handle the shooting of an enemy on the enemy object? Wouldn't that make more sense?

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 nikocr33nzz · Nov 26, 2016 at 01:35 AM 0
Share

@bunny83 : yes, normally i would put the shooting in the enemy gameobject. but in this case i want to try to make 1 script that contains all the enemy actions. then when the enemy instantiate they can chose one or two action from the rulebase script.

so, can you fix the script up there please ? what do i have to add in the script to work? thanks for respons

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I make do I make a variable go back to its original value for a spell system 1 Answer

Having trouble using SendMessage 1 Answer

3rd Person Camera to Rotate With the Player 0 Answers

Need help fixing my script 1 Answer

How do I make my projectile deal damage to a target it hit. 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