Search Unity

Space Shooter turorial: Instantiate not working for me Unity 5

Discussion in 'Scripting' started by Danicano, Mar 10, 2015.

  1. Danicano

    Danicano

    Joined:
    Mar 10, 2015
    Posts:
    18
    Dowloaded Unity 5 and followed Space Shooter tutorial (http://unity3d.com/learn/tutorials/projects/space-shooter/shooting-shots). Get stucked on Instantiate the laser shots, not working for me.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class pbehaviour : MonoBehaviour {
    5.  
    6.     public float speed, xMin,xMax,zMin,zMax,tilt, fireRate;
    7.     public GameObject shot;
    8.     public Transform shotSpawn;
    9.     private float nextFire;
    10.  
    11.     void FixedUpdate(){
    12.         float moveHorizontal = Input.GetAxis ("Horizontal");
    13.         float moveVertical = Input.GetAxis ("Vertical");
    14.  
    15.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    16.         GetComponent<Rigidbody>().velocity = movement * speed;
    17.         GetComponent<Rigidbody>().position = new Vector3 (
    18.             Mathf.Clamp(GetComponent<Rigidbody>().position.x,xMin,xMax),
    19.             0.0f,
    20.             Mathf.Clamp(GetComponent<Rigidbody>().position.z, zMin,zMax)
    21.             );
    22.         GetComponent<Rigidbody> ().rotation = Quaternion.Euler (0.0f,0.0f , GetComponent<Rigidbody> ().velocity.x * -tilt );
    23.     }
    24.  
    25.     void update(){
    26.  
    27.         if (Input.GetKeyDown("space") && Time.time > nextFire) {
    28.             nextFire = Time.time + fireRate;
    29.             Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
    30.         }
    31.     }
    32. }

    Is a new way to write it ???
     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    There is no new way to write it.

    Do you can an error of any kind in the console? Have you tried using Debug.Log to check if all code gets executed properly? Are all the references set, are the fields filled in the inspector?
     
  3. Danicano

    Danicano

    Joined:
    Mar 10, 2015
    Posts:
    18
    I'll check out. The main reason for this post was if the code is well written, for the expressios of the type 'rigidbody.velocity....' have been automatically updated to 'GetComponent<Rigidbody>.....'. If the code is right I'll recheck everything. Thanks for the reply.