- Home /
Error CS0103?
Hi,
I'm getting a CS0103 error, "The name "ballPosition" does not exist it the current context" on line 25, A.k.a: ballPostition = transform.position + new Vector3(0, 1f, 0);
Here is my code:
using UnityEngine;
using System.Collections;
public class paddleScript : MonoBehaviour {
float paddleSpeed = 10f;
public GameObject ballPrefab;
GameObject attachedBall = null;
// Use this for initialization
void Start () {
SpawnBall();
}
void SpawnBall() {
//Spawns the ball, duh!
ballPostition = transform.position + new Vector3(0, 1f, 0);
Quaternion ballRotation = Quaternion.identity;
attachedBall = (GameObject)Instantiate( ballPrefab, ballPostition, ballRotation );
}
// Update is called once per frame
void Update () {
transform.Translate ( paddleSpeed * Time.deltaTime * Input.GetAxis ( "Horizontal" ), 0, 0 );
if( attachedball ) {
attachedBall.rigidbody = transform.position + new Vector3(0, .75f, 0);
}
if( Input.GetButtonDown( "Jump" ) ){
//Fire the ball!
if( attachedball ) {
attachedBall.rigidbody.isKinematic = false;
attachedBall.rigidbody.Addforce(0, 300f, 0);
attachedBall = null;
}
}
}
void OnCollisionEnter( Collision col ) {
foreach (ContactPoint contact in col.contacts) {
if( contact.thisCollider == collider ) {
float english = contact.point.x - transform.position.x;
contact.otherCollider.rigidbody.AddForce( 300f * english, 0, 0);
}
}
}
}
Answer by EHogger · Feb 12, 2013 at 12:12 AM
Variables in c# have to be declared as a certain type before they can be assigned. You need to add something like:
private Vector3 ballPosition;
private Quaternion ballRotation;
In the same place that you (correctly) declared ballPrefab and attachedBall.
Answer by slader166 · Feb 14, 2013 at 03:04 AM
Sorry, I'm used to javascript. Where exactly would I add those? From what I can tell I declared those at line 25 and line 5.
I think you meant to comment on EHoggers answer. Please make sure you comment next time.. You can put those variables anywhere in the class, outside of any methods. It's customary to put them right after the opening bracket of the class.
I added it to line 26 and I am getting another error, unexpected symbol "private".
They must be placed before you try to use them. Try placing them on line 5.
I fixed the errors, I spelled it "postition" ins$$anonymous$$d of "position", LOL. But I am getting a new error: paddleScript.cs(41,31): error CS0200: Property or indexer `UnityEngine.GameObject.rigidbody' cannot be assigned to (it is read only). How do I fix that?
Your answer

Follow this Question
Related Questions
Problem with gun/shooting scripting 2 Answers
Unexpected token: targetPos 1 Answer
Could not preload global game manager #0 3 Answers
error CS1502 and error CS1503 What is this? 1 Answer
Could not preload global game manager #0 0 Answers