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 jesser1981 · Mar 06, 2014 at 03:42 PM · gameobject

Several "lazers" fire and never destroy themselves

I am trying to make a space shooter game and I have gotten to the point where I am trying to get the player to be able to push the spacebar and a lazer fires from the ship. As soon as I push play it automatically fires a bunch of the lazer(clones) without me even pushing spacebar and it doesn't destroy them after they pass the point I have indicated. Here is the script.`using UnityEngine; using System.Collections;

public class LazerFab : MonoBehaviour {

 private Transform myTransform;
 public int projectileSpeed = 7;

 void Start (){
     myTransform = transform;
     }

 
 // Update is called once per frame
 void Update () {
 

     myTransform.Translate (Vector3.up * projectileSpeed * Time.deltaTime);

     //Destroy lazer when it reaches the top of the screen

     if (myTransform.position.y >= 7)
     {
         DestroyObject (this.gameObject); //destroys lazer
     }
 
 }

}`

is the lazer sript and the player script is this using UnityEngine; using System.Collections;

 public class Player : MonoBehaviour {
 
     private Transform myTransform;
     public int playerSpeed = 5;
 
     public GameObject LazerFab;
 
     // Use this for initialization
     void Start () {
 
         myTransform = transform;
 
             //start point  x  y  z
             //position at -3 -3 -1
         myTransform.position = new Vector3 (-3, -3, -1);
 
     
     }
     
     // Update is called once per frame
     void Update () {
 
         //move player left and right along x axis
         myTransform.Translate (Vector3.right * playerSpeed * Input.GetAxis("Horizontal") * Time.deltaTime);
 
         //if the player is at -11 on the right of the screen then the player should appear at 11 and vice versa
         if (myTransform.position.x >= 10) {
                         myTransform.position = new Vector3 (-10, myTransform.position.y, myTransform.position.z);
                 }
         else if (myTransform.position.x <= -10) 
                 {
             myTransform.position = new Vector3 (10, myTransform.position.y, myTransform.position.z);
                 }
         //press spacebar to fire weapons
         //if the player presses the spacebar a projectile will fire from certain points
 
         if (Input.GetKeyDown ("space"))
             Debug.Log ("spacebar was pressed");
             //fire weapons
         {
             Instantiate(LazerFab);
         }
 
 
     
     }
 }
 
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
Best Answer

Answer by FinKone · Mar 06, 2014 at 04:04 PM

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StartCoroutine.html

In line 37 of player script try Keycode.Space instead of "space". Make sure the distance check in the lazer script is correctly linked to the player transform for the distance check.

Any prefab that you create will need a link established in script to tell it what distance its checking in order to destroy itself.

Perhaps using a time based destroy instead of making a link to the player object and distance checking it would be easier. Check the link above for learning that. My phone didn't want to paste it at the bottom.

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

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

21 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

Related Questions

Coding Gui Text To Perform After GameObject Destroy? 1 Answer

Adding component to component 1 Answer

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

Problem with PlayerHealth and renderer 1 Answer

C# Preserving GameObjects' Previous Meshes 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