Search Unity

where errors

Discussion in 'Scripting' started by XLIVE99, Aug 12, 2014.

  1. XLIVE99

    XLIVE99

    Joined:
    Aug 12, 2014
    Posts:
    14
    I'm from turkey and I can't speak english.I am do a 2.5d game but I Can't rotate a gun.This my script and error:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class silahhareket : MonoBehaviour {
    4.     // Use this for initialization
    5.     void Start () {
    6.  
    7.     }
    8.  
    9.     // Update is called once per frame
    10.     void Update () {
    11.         //rotation
    12.         Vector3 mousePos = Input.mousePosition;
    13.         mousePos.z = 5.42f;
    14.      
    15.         Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
    16.         mousePos.x = mousePos.x - objectPos.x;
    17.         mousePos.y = mousePos.y - objectPos.y;
    18.      
    19.         float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
    20.         transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    21.     }
    22. }
    23.  
    error:
    NullReferenceException: Object reference not set to an instance of an object
    silahhareket.Update () (at Assets/silah ayarları/silahhareket.cs:17)

    and I have a different scripts it's a fire scripts But not a cube fire because I can't find the cube fire gun(if you know a cube fire gun please write the scripts):

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class fire : MonoBehaviour {
    5.  
    6.     public float fireRate = 0;
    7.     public float damage = 10;
    8.     public LayerMask notToHit;
    9.  
    10.     float timeToFire = 0;
    11.     Transform firePoint;
    12.  
    13.     // Use this for initialization
    14.     void Awake () {
    15.         firePoint = transform.FindChild ("FirePoint");
    16.         if (firePoint == null) {
    17.             Debug.LogWarning ("No firePoint? What?!");
    18.         }
    19.     }
    20.     // Update is called once per frame
    21.     void Update () {
    22.         if (fireRate == 0) {
    23.                         if (Input.GetButtonDown ("Fire1")) {
    24.                             shoot();
    25.                         }
    26.                 } else {
    27.             if (Input.GetButton ("Fire1") && Time.time > timeToFire) {
    28.                 timeToFire = Time.time + 1/fireRate;
    29.                 shoot();
    30.             }
    31.         }
    32.     }
    33.     void shoot () {
    34.         Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
    35.         Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
    36.         RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, notToHit);
    37.         Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
    38.         if (hit.collider != null) {
    39.             Debug.Log ("test");
    40.             Debug.DrawRay (firePointPosition, hit.point, Color.red);
    41.             }
    42.         }
    43. }
    error:

    NullReferenceException: Object reference not set to an instance of an object
    fire.shoot () (at Assets/silah ayarları/fire.cs:35)
    fire.Update () (at Assets/silah ayarları/fire.cs:25)

    (Sorry I can some speak english)
     
    Last edited: Aug 13, 2014
  2. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Use the Insert Code button and check indentation, please
     
  3. XLIVE99

    XLIVE99

    Joined:
    Aug 12, 2014
    Posts:
    14
    what is this?
     
  4. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    When you edit a message, the button between the film icon and the floppy disk icon serve to paste code like this:
    Code (CSharp):
    1.  
    2. if (Input.GetButton ("Fire1") && Time.time > timeToFire) {
    3.     timeToFire = Time.time + 1/fireRate;
    4.     shoot();
    5. }
     
  5. XLIVE99

    XLIVE99

    Joined:
    Aug 12, 2014
    Posts:
    14
    please help meee
     
  6. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    The first script works with no error. The seconds works too but you have to add an empty gameObject called "FirePoint" as a child representing the weapon position.
     
  7. XLIVE99

    XLIVE99

    Joined:
    Aug 12, 2014
    Posts:
    14
    scripts not work.
    first scripts error:
    NullReferenceException: Object reference not set to an instance of an object
    silahhareket.Update () (at Assets/silah ayarları/silahhareket.cs:17)

    second scripts error:
    NullReferenceException: Object reference not set to an instance of an object
    fire.shoot () (at Assets/silah ayarları/fire.cs:35)
    fire.Update () (at Assets/silah ayarları/fire.cs:25)
     
    Last edited: Aug 20, 2014
  8. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    For the first script you need at least one camera in the scene with tag "MainCamera".
    For the second script:
    1. From the menu: GameObject\Create Empty
    2. rename the GameObject "FirePoint".
    3. Drag the FirePoint on the object where the Fire.cs is attached.
    If you still see in the log "No firePoint? What?!" then you did something wrong in the 3 points above.
     
  9. XLIVE99

    XLIVE99

    Joined:
    Aug 12, 2014
    Posts:
    14


    Not Work again :(
     
  10. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Can you check what are the lines 17 and 35 please? Are exactly the same as in your first post?
    This is 17? mousePos.y= mousePos.y- objectPos.y;
    This is 35? Vector2 firePointPosition =newVector2(firePoint.position.x, firePoint.position.y);
     
  11. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    The only thing that give me an error is if I change the MainCamera Tag to something else than "MainCamera". So double check in the inspector that all your Cameras have the correct tag.
    Other than that I changed the first line of shoot() because ScreenToWorldPoint need a third parameter too (distance from camera). Otherwise return always the same value.

    Code (CSharp):
    1.     void shoot () {
    2.         Vector3 mp = Input.mousePosition;
    3.         mp.z = 10; // z = Distance from the camera
    4.         Vector2 mousePosition = Camera.main.ScreenToWorldPoint (mp);
    5.         Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
    6.         RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, notToHit);
    7.         Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
    8.         if (hit.collider != null) {
    9.             Debug.Log ("test");
    10.             Debug.DrawRay (firePointPosition, hit.point, Color.red);
    11.         }
    12.     }
    13.  
     
  12. XLIVE99

    XLIVE99

    Joined:
    Aug 12, 2014
    Posts:
    14
    Not work this is the pictures:
     
  13. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You have no variable declarations for firePoint or notToHit. It looks like you're literally copy-pasting code from the forum into your script and expecting it to work, which it rarely does - as you've experienced. Try to debug it yourself.
     
  14. XLIVE99

    XLIVE99

    Joined:
    Aug 12, 2014
    Posts:
    14
    I can't do this I don't know the code because I don't know the english I know the turkish and I can't find code description turkish
     
  15. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    So you're going to rely on the folks in this forum to write the code for your game? I understand the frustrations of being an ESL programmer but come on now...
     
  16. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    I gave you the part I have changed but of course you have to include it into what you already have...
    And that was in the fire.cs!
     
    Last edited: Aug 22, 2014
  17. XLIVE99

    XLIVE99

    Joined:
    Aug 12, 2014
    Posts:
    14
    I forget what. How do you texture?