Search Unity

API updating script cause them to no longer work.

Discussion in 'Physics' started by Shobosa, Sep 1, 2015.

  1. Shobosa

    Shobosa

    Joined:
    Jul 8, 2015
    Posts:
    18
    I am working through the tutorials to increase my level of knowledge of unity, but I am running into issue on some of them saying "the project contains scripts that use obsolete API. When I do they update the script doesn't function. Can someone look at this code and help me out?

    before update API:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Resetter : MonoBehaviour {
    6.  
    7.    public Rigidbody2D projectile;
    8.    public float resetSpeed = 0.025f;
    9.    
    10.    private float resetSpeedSqr;
    11.    private SpringJoint2D spring;
    12.    
    13.    
    14.    void Start () {
    15.      resetSpeedSqr = resetSpeed * resetSpeed;
    16.      spring = projectile.GetComponent <SpringJoint2D>();
    17.      
    18.    }
    19.    
    20.    
    21.    void Update () {
    22.      if(Input.GetKeyDown (KeyCode.R)) {
    23.        Reset();
    24.      }
    25.      if(spring == null && projectile.velocity.sqrMagnitude < resetSpeedSqr){
    26.        Reset();
    27.      }
    28.      
    29.    }
    30.    
    31.    void OntriggerExit2D (Collider2D other){
    32.      if (other.rigidbody2D == projectile){
    33.        Reset();
    34.      }
    35.    }
    36.    void Reset () {
    37.      Application.LoadLevel (Application.loadedLevel);
    38.    }
    39. }
    40.  
    41.  
    After API update
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Resetter : MonoBehaviour {
    6.    public Rigidbody2D projectile;
    7.    public float resetSpeed = 0.025f;
    8.    
    9.    private float resetSpeedSqr;
    10.    private SpringJoint2D spring;
    11.    
    12.  void Start () {
    13.      resetSpeedSqr = resetSpeed * resetSpeed;
    14.      spring = projectile.GetComponent <SpringJoint2D>();
    15.      
    16.    }
    17.    
    18.    void Update () {
    19.      if(Input.GetKeyDown (KeyCode.R)) {
    20.        Reset();
    21.      }
    22.      if(spring == null && projectile.velocity.sqrMagnitude < resetSpeedSqr){
    23.        Reset();
    24.      }
    25.      
    26.    }
    27.    
    28.    void OntriggerExit2D (Collider2D other){
    29.      if (other.GetComponent<Rigidbody2D>() == projectile){
    30.        Reset();
    31.      }
    32.    }
    33.    void Reset () {
    34.      Application.LoadLevel (Application.loadedLevel);
    35.    }
    36. }
    37.  
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Try other.gameObject.getcomponent instead of other.getcomponent
     
  3. Shobosa

    Shobosa

    Joined:
    Jul 8, 2015
    Posts:
    18
    1. void OntriggerExit2D (Collider2D other){
    2. if (other.GetComponent<Rigidbody2D>() == projectile){
    3. Reset();
    4. }
    to this?
    Code (csharp):
    1.  
    2. [LIST=1]
    3. [*] void OntriggerExit2D (Collider2D other){
    4. [*]     if (other.gameObject.getcomponent<Rigidbody2D>() == projectile){
    5. [*]       Reset();
    6. [*]     }
    7. [/LIST]
    8.  
    ??
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
  5. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Or simply let the getcomponent part out, if you store the projectile variable as a gameobject
     
  6. Shobosa

    Shobosa

    Joined:
    Jul 8, 2015
    Posts:
    18
    Code (csharp):
    1.  
    2. void OntriggerExit2D (Collider2D other){
    3. if (other.gameObject.getcomponent<Rigidbody2D>() == projectile){
    4.   Reset();
    5. }
    6.  
    didn't work. I think I am giving up. Sad when the tutorials for the product don't work.
     
  7. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    First of all, GetComponent, not getcomponent, secondly, let the whole thing out, simply do other.gameObject

    Edit.: wait, did you assign the projectile to the variable?
     
    Last edited: Sep 4, 2015