- Home /
Space Shooter Tutorial Error CS1061
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed;
public float xMin, xMax, zMin, zMax;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.velocity = movement * speed;
rb.position = new Vector3(Mathf.Clamp(rigidbody.position.x, xMin, xMax), 0.0f, Mathf.Clamp(rigidbody.position.x, zMin, zMax));
}
}
I'm following the space shooter tutorial on the unity learn page, using Unity 5, but the tutorial was made on Unity 4, sooo obviously there are differences (also considering the tutorial was made on a Mac and I'm using Windows).
After updating my script, I returned to Unity and got two errors on the same line:
"Assets/Scripts/PlayerController.cs(25,57): error CS1061: Type UnityEngine.Component' does not contain a definition for
position' and no extension method position' of type
UnityEngine.Component' could be found. Are you missing an assembly reference?"
"Assets/Scripts/PlayerController.cs(25,110): error CS1061: Type UnityEngine.Component' does not contain a definition for
position' and no extension method position' of type
UnityEngine.Component' could be found. Are you missing an assembly reference?"
the only difference between the two errors is the second number in the parentheses. I know the first number tells you which line the error is referring to, but I have no idea what the second number is for.
What are the errors telling me to do/how would I do it? What's the fix here? I looked at the update guide for the videos but I can't see any fixes for it.
Answer by eskivor · Jun 20, 2017 at 11:36 PM
TL;DR : at lines 25 and 26 replace rigidbody
by rb
In Unity 5, you cannot use anymore directly rigidbody
as a shortcut to reference the component Rigidbody
, using rigidbody
was equivalent to use GetComponent<Rigidbody>()
. Now using the short version rigidbody
is impossible (to avoid users to use too much the GetComponent
function (which can be consuming) when it's unnecessary, because they ignored they were using GetComponent
functions). You have to use instead GetComponent<Rigidbody>()
here to get the Rigidbody
component attached to the gameObject. But you are already using the variable rb
to get the Rigidbody
component, so you can simply use rb
here.
You can also report the bug of the tutorial to Unity
Huh. I completely passed over those rbs. Guess I need to look a bit more closely. Thanks for the help. :D
One more thing. After fixing the error, I went back into Unity and tested it, and now it's doing really strange things. When I try to move upwards, the ship moves up a bit and stops two units, then when I release the up key, it goes back down to its original position. When I press down it does the same thing in the other direction. When I press right, it moves towards the top right limit then moves right 2 units, then goes left 2 units when I release the right key. When I press left, it goes to the bottom left limit, then 2 units left, and 2 units right when I release.
Here's my updated script (boundaries are x$$anonymous$$in = -6, x$$anonymous$$ax = 6, z$$anonymous$$in = -4, z$$anonymous$$ax = 8) : using System.Collections; using System.Collections.Generic; using UnityEngine;
[System.Serializable]
public class Boundary
{
public float x$$anonymous$$in, x$$anonymous$$ax, z$$anonymous$$in, z$$anonymous$$ax;
}
public class PlayerController : $$anonymous$$onoBehaviour {
public float speed;
public Boundary boundary;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.velocity = movement * speed;
rb.position = new Vector3($$anonymous$$athf.Clamp(rb.position.x, boundary.x$$anonymous$$in, boundary.x$$anonymous$$ax), 0.0f, $$anonymous$$athf.Clamp(rb.position.x, boundary.z$$anonymous$$in, boundary.z$$anonymous$$ax));
}
}
Your answer
Follow this Question
Related Questions
Compiling error: AndroidJavaClass and AndroidJavaObject problems 0 Answers
float to string c# unity script Android csharp 2 Answers
Trouble wth Error CS1061 2 Answers
Targetting script errors 1 Answer
INTERNAL COMPILER ERROR 0 Answers