- Home /
Tutorial Problems
I'm going through the Space Shooter tutorial on the Unity site to prepare to make my own game. As I was proceeding to run the game I kept getting this error "Assets/Scripts/PlayerController.cs(18,17): error CS1922: A field or property UnityEngine.Vector3' cannot be initialized with a collection object initializer because type
UnityEngine.Vector3' does not implement System.Collections.IEnumerable' interface" Here is the code.
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour { public float speed; public float xMin , xMax, zMin, zMax;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
GetComponent<Rigidbody>().velocity = movement * speed;
GetComponent<Rigidbody>().position = new Vector3
{
Mathf.Clamp( Rigidbody.position.x, xMin,xMax),
0.0f,
Mathf.Clamp( Rigidbody.position.z, zMin, zMax)
};
}
}`
Use parenthesis when creating new objects like on line 7. On line 11/15 you are accidentally using curley brackets.
I used () but Unity popped up a bunch of errors. I substituted line 11 with () and line 15 with () as well but got multiple errors. So what exactly do you mean?
When you get errors, double click the top error and fix it, save the file, compile it again, and keep working your way down the list like that until the other errors are resolved.
Answer by Lo0NuhtiK · May 24, 2015 at 05:30 PM
public float speed; public float xMin , xMax, zMin, zMax;
//cache for <Rigidbody>
Rigidbody rb ;
void Start()
{
//cache our Rigidbody
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
//bad brackets you had here... {} != ()
(
Mathf.Clamp( rb.position.x, xMin,xMax),
0.0f,
Mathf.Clamp( rb.position.z, zMin, zMax)
);
}
(
$$anonymous$$athf.Clamp (Rigidbody.position.x, x$$anonymous$$in, x$$anonymous$$ax),
0.0f,
$$anonymous$$athf.Clamp (Rigidbody.position.z, z$$anonymous$$in, z$$anonymous$$ax)
);
}
It seems to have worked for the most part but I get a parsing error on line 30 ( line below curly bracket) even though there is nothing there. Oh and yes I tried to add 2 more curl brackets.
FIXED! I sat there and figured it out , pretty proud of myself .lol. But on the real thanks for helping me out with the previous error.
Your answer
Follow this Question
Related Questions
Common way to make a GameTutorial? 1 Answer
2D Game example 2 Answers
Best way to make a bullet hell shooter in Unity? 1 Answer
Roll-A-Ball Problem 2 Answers
Need Help in adding UI text in Unity 4.5 0 Answers