function n = NumberOfNeighbours(ScriptVar, x, y, OldArray, particle_index)
    % Calculate number of neighbours for the given particle
    % Output -
    %    n = Number of neighbours
    % 
    % Inputs - 
    %    ScriptVar - Parameters container map
    %    OldArray - Array of particle coordinates for the current frame
    %    particle_index - Number of neighbours is calculated for this particle 
    
    rSense = ScriptVar('rSense');
    N_Particles = ScriptVar('N_Particles');
    [TotalParticles, ~] = size(OldArray);
    
    NumNeigh = zeros(TotalParticles,1);
    ParticleType = ParticleTypeCalculator(ScriptVar, particle_index); 
    oldarray_x = OldArray(:,1);
    oldarray_y = OldArray(:,2);
    radius = rSense(ParticleType);
    parfor i = 1:TotalParticles
        if (x-oldarray_x(i))^2 + (y-oldarray_y(i))^2 <= radius^2
            NumNeigh(i) = 1;
        end
    end
    NumNeigh_new = zeros(size(N_Particles));
    for i = 1:TotalParticles
        if NumNeigh(i) == 1
            k = ParticleTypeCalculator(ScriptVar,i);
            NumNeigh_new(k) = NumNeigh_new(k) + 1;
        end
    end
    % Notice that at the end of the for loop above, numneigh >= 1. This is
    % because the particle whose neighbours are being calculated, itself
    % satisfies the if condition.
    NumNeigh_new(ParticleType) = NumNeigh_new(ParticleType)-1;
    n = NumNeigh_new;
end