- Home /
Question by
DoctorBees · Sep 24, 2014 at 02:27 PM ·
c#context
The name hitFactor does not exist in the current context
I'm getting error CS0103 for lines 17 and 31 both saying: "The name hitFactor does not exist in the current context" Thanks, I'm just getting started with C#
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
// Speed to be modified in the Inspector
public float speed = 2.0f;
void Start() {
// Give the ball some initial movement direction
rigidbody2D.velocity = Vector2.one.normalized * speed;
}
void OnCollisionEnter2D(Collision2D col) {
// Hit the left Racket?
if (col.gameObject.name == "RacketLeft") {
// Calculate hit Factor
float y=hitFactor(transform.position,
col.transform.position,
((BoxCollider2D)col.collider).size.y);
// Calculate direction, set length to 1
Vector2 dir = new Vector2(1, y).normalized;
// Set Velocity with dir * speed
rigidbody2D.velocity = dir * speed;
}
// Hit the right Racket?
if (col.gameObject.name == "RacketRight") {
// Calculate hit Factor
float y=hitFactor(transform.position,
col.transform.position,
((BoxCollider2D)col.collider).size.y);
// Calculate direction, set length to 1
Vector2 dir = new Vector2(-1, y).normalized;
// Set Velocity with dir * speed
rigidbody2D.velocity = dir * speed;
}
}
}
Comment
it is telling the truth it does not exist in the current context
When you're copying the code from a tutorial, make sure you include everything!
The class haven't defined a hitFactor method, but you're trying to use it. If you go back to your Unity 2D Pong Game tutorial, it's defined just above the OnCollisionEnter2D you have there.
Your answer
![](https://koobas.hobune.stream/wayback/20220613163708im_/https://answers.unity.com/themes/thub/images/avi.jpg)