Search Unity

My initiate function is going off without me pressing anything

Discussion in 'Editor & General Support' started by NaveGCT, Jul 6, 2018.

  1. NaveGCT

    NaveGCT

    Joined:
    Dec 20, 2017
    Posts:
    5
    My InstantiateFunction isnt working for my game. is t his code correct? the game is Unity2D
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour {
    6.  
    7.     public float speed = 10.0f;
    8.     public Transform laserprefab;
    9.  
    10.    
    11.  
    12.               // Use this for initialization
    13.               void Start () {
    14.         Debug.Log("Start is called");
    15.         Debug.Log("Player " + name + " Loaded!");
    16.     }
    17.              
    18.               void Update ()
    19.     {
    20.         Movement();
    21.         laserprefab.position = new Vector3(transform.position.x, laserprefab.position.y, laserprefab.position.z);
    22.         Attacks();
    23.     }
    24.  
    25.     void Movement ()
    26.     {
    27.         float horizantalINP = Input.GetAxis("Horizontal");
    28.         Debug.Log("Frames Ran:");
    29.         Debug.Log("X:" + transform.position.x + " Y:" + transform.position.y + " Z:" + transform.position.y);
    30.         transform.Translate(Vector3.right * Time.deltaTime * speed * horizantalINP);
    31.         if (transform.position.x > 9.5f)
    32.         {
    33.             transform.position = new Vector3(9.5f, transform.position.y, 0);
    34.         }
    35.         else if (transform.position.x < -9.5f)
    36.         {
    37.             transform.position = new Vector3(-9.5f, transform.position.y, 0);
    38.         }
    39.     }
    40.  
    41.     void Attacks()
    42.     {
    43.         if (Input.GetKeyUp(KeyCode.C)) ;
    44.         {
    45.             Instantiate(laserprefab);
    46.         }
    47.         if (Input.GetKey(KeyCode.Space)) ;
    48.         {
    49.             Instantiate(laserprefab);
    50.         }
    51.     }
    52. }
    53.  
     
  2. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    951
    As far as I can tell, this should work. What does the code do, and what do you expect it to do?
     
  3. NaveGCT

    NaveGCT

    Joined:
    Dec 20, 2017
    Posts:
    5
    sorry, i fixed it. thanks. i was trying to get it to add the laser to the screen ON THE FRONT part. it was just going on the back part the whole time.