Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Rail Grinding For Sonic Game

Discussion in 'General Discussion' started by mxshii, Mar 29, 2020.

  1. mxshii

    mxshii

    Joined:
    Mar 6, 2020
    Posts:
    3
    Hey Guys i am making a sonic game, anyways i coded rail grinding but when i play the scene and try to grind it gives me null reference exception can someone help
    here is the code in the line 93
    Code (CSharp):
    1. public Rail rail { get; set; }
    2.     PlayerBhysics Player;
    3.     ActionManager Actions;
    4.     public Animator CharacterAnimator;
    5.  
    6.     public int currentSeg { get; set; }
    7.     float transition;
    8.     bool isCompleted;
    9.     Quaternion CharRot;
    10.     int railActiveCount;
    11.  
    12.     void Awake()
    13.     {
    14.         Actions = GetComponent<ActionManager>();
    15.         Player = GetComponent<PlayerBhysics>();
    16.     }
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         railActiveCount += 1;
    21.  
    22.         if (rail != null)
    23.         {
    24.             OnRail(Player.SpeedMagnitude/100);
    25.             for (float i = 0; i < Player.SpeedMagnitude; i++)
    26.             {
    27.                 //OnRail(Player.SpeedMagnitude/100);
    28.             }
    29.             Vector3 resety = Player.rigidbody.velocity;
    30.             resety.y = 0;
    31.             Player.rigidbody.velocity = resety;
    32.  
    33.             //Get Out of rail
    34.             if(Actions.Action == 1)
    35.             {
    36.                 rail = null;
    37.             }
    38.  
    39.         }
    40.     }
    41.  
    42.     void Update()
    43.     {
    44.         if (rail != null)
    45.         {
    46.             if (Input.GetButton("A") && railActiveCount > 10)
    47.             {
    48.                 Actions.Action01.InitialEvents();
    49.                 Actions.ChangeAction(1);
    50.                 railActiveCount = 0;
    51.             }
    52.         }
    53.     }
    54.  
    55.     void OnRail(float speed)
    56.     {
    57.         //Get out when over
    58.  
    59.         if (currentSeg >= (rail.RailArray.Length -1))
    60.         {
    61.             rail = null;
    62.             return;
    63.         }
    64.         else if (currentSeg <= 0)
    65.         {
    66.             rail = null;
    67.             return;
    68.         }
    69.  
    70.  
    71.         transition += speed;
    72.  
    73.         if(transition > 1)
    74.         {
    75.             transition = 0;
    76.             currentSeg++;
    77.         }
    78.         else if(transition < 0)
    79.         {
    80.             transition = 1;
    81.             currentSeg--;
    82.         }
    83.  
    84.         transform.position = rail.LinearPosition(currentSeg, transition);
    85.  
    86.     }
    87.  
    88.     public void OnCollisionEnter(Collision col)
    89.     {
    90.         if(col.gameObject.tag == "Rail")
    91.         {
    92.             ////Debug.Log("Col");
    93.             if(col.gameObject.transform.parent.GetComponent<Rail>() != null)
    94.             {
    95.                 if (railActiveCount > 20 && rail == null)
    96.                 {
    97.                     rail = col.gameObject.transform.parent.GetComponent<Rail>();
    98.                     currentSeg = GetClosestPos(col.gameObject.transform.parent.GetComponent<Rail>().RailArray, transform.position);
    99.                     Actions.ChangeAction(5);
    100.                     railActiveCount = 0;
    101.                 }
    102.             }
    103.         }
    104.     }
    105.  
    106.     public int GetClosestPos(Vector3[] pos, Vector3 playerPos)
    107.     {
    108.         int seg = 0;
    109.         Vector3 tMin = Vector3.zero;
    110.         float minDist = Mathf.Infinity;
    111.         Vector3 currentPos = playerPos;
    112.         for(int i = 0; i < pos.Length; i++)
    113.         {
    114.             float dist = Vector3.Distance(pos[i], currentPos);
    115.             if (dist < minDist)
    116.             {
    117.                 seg = i;
    118.                 minDist = dist;
    119.             }
    120.         }
    121.         return seg;
    122.     }
    123.  
    124. }
    125.  
     
  2. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,886
    This is not a support forum, post in scripting section.
    Obviously either the gameObject, its parent, or the rail component references were unable to resolve hence the null reference. You need to debug your code to find out why, either you set up the object wrong or more likely its grabbing another object instead of the one you expect.
     
    mxshii likes this.
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    Code (csharp):
    1.  
    2. f(col.gameObject.transform.parent.GetComponent<Rail>() != null)
    3.  
    Assuming that collision has gameobject, and that gameobject has parent is kinda bold.

    Most likely scenario there's no parent, and that's why it crashes.
     
    mxshii likes this.
  4. mxshii

    mxshii

    Joined:
    Mar 6, 2020
    Posts:
    3
    Great thx
     
  5. mxshii

    mxshii

    Joined:
    Mar 6, 2020
    Posts:
    3
    thx very much