Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
So i use unity and trying to use Rigidbody scripts and popped up with 5 errors with CS1003.
Heres my code:
using UnityEngine;
public class Movement : MonoBehaviour
void Start ()
rb.useGravity false;
{ public Rigidbody rb;
void Update() {
rb.AddForce(0, 0, 1000*Time.deltaTime);
}
}
I tried putting brackets in certain places but couldnt figure out what to put.
using UnityEngine;
public class Movement : MonoBehaviour
{
public Rigidbody rb;
void Start()
{
rb.useGravity = false;
}
void Update()
{
rb.AddForce(0, 0, 1000 * Time.deltaTime);
}
}
I think you want it to look a little like the above.
You are correct that your bracket closure is one of the things stopping Unity from being able to compile your program.
It seems that you are not very familiar with C# syntax in general. I would suggest reading up on these basics, it will greatly help you in developing in Unity.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 days ago.
Improve this question
I am aware this is a novel issue and it is just me either misreading the code or just forgetting a character but I cannot for the life of me figure out where this semi colon is actually meant to go if there even is a need for another one. I get the error stated in the title as well as the lines '(21,10)' but as far as I'm aware, all the correct semi colons are in place. I apologise as this is something I should probably be able to figure out but syntax errors like these seem to be the bane of my existence lately.
Code shown below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pathfinding;
public class EDPathfinding : MonoBehaviour
{
public AIPath aipath;
void Start()
{
}
// Update is called once per frame
void Update()
{
if(aipath.desiredVelocity.x => 0.01f)
{
transform.localScale = new Vector3(-1f, 1f, 1f);
}
else if (aipath.desiredVelocity.x <= -0.01f)
{
transform.localScale = new Vector3(-1f, 1f, 1f);
}
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I am working on a game made in Unity Engine.
For Movement, I have used vector2. But for vector2, you need to spam the buttons in order for the player to move. So i tried "while" function to loop the process. Here is the main code
if (Input.GetKeyDown(KeyCode.W))
{
i = 5;
}
//test
if (Input.GetKeyUp(KeyCode.W))
{
i = 1;
}
while(i !=1)
{
rb.AddForce(Vector2.up * JumpForce);
}
However, when I run it the engine crashes. Why?
Just to let you know, there are no compiler errors.
In Unity, the physics movement should be done in FixedUpdate, and input checking in Update. Something like this:
[SerializeField] private Rigidbody _rb;
[SerializeField] private float _jumpForce;
private bool _addForce;
private void Update()
{
_addForce = Input.GetKey(KeyCode.W);
}
private void FixedUpdate()
{
if (_addForce) _rb.AddForce(Vector2.up * _jumpForce);
}
If you add a while in the Update, it will stuck in that frame, freezing the main loop, and the engine can't poll for new inputs, so your Update can't continue.
The repeated addforces might be a bit much for the PC.
Do this:
if (Input.GetKey(KeyCode.W))
{
rb.AddForce(Vector2.up * JumpForce);
}
This way it only moves when key is being held.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm following a unity practice from youtube and seem to have problems at 6:42.
here's the link: https://www.youtube.com/watch?v=gE7gc1sblUA/
and a screenshot:
and the script:
using UnityEngine;
public class player : MonoBehaviour {
public float jumpforce = 10f;
public rigidbody2D rb;
// Update is called once per frame
void Update() {
if (input.GetButtonDown("Jump") || Input.GetMouseButtonDown(0))
{
rb.velocity = Vector2.up * jumpforce;
}
}
}
How can I solve this?
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want my little cube to change to a bigger one when i click the upper arrow, and change back when i pres the down arrow. I have tried:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerSkift : MonoBehaviour {
public gameObject myObject1;
public gameObject myObject2;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.UpArrow))
{
myObject1.SetActive (false);
myObject2.SetActive (true);
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
myObject2.SetActive(false);
myObject1.SetActive(true);
}
}
}
When i try to run it it says:
Assets/PlayerSkift.cs(9,9): error CS0118: `UnityEngine.Component.gameObject' is a `property' but a `type' was expected
I have no idea what that means, so if you know it, or know how to do it in a other way. Please help.
public gameObject myObject1;
public gameObject myObject2;
the above code, as per this discussion, appears to have a typo.
gameObject should be GameObject.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am new to unity but I have an error that appears whenever I create a variable.
It says error cs1525: unexpected symbol 'public'
here is my script
using UnityEngine;
using System.Collections;
public class move : MonoBehaviour
{
// Use this for initialization
void Start ()
{
public float speed = 3.0f;
}
// Update is called once per frame
void Update ()
{
}
}
Class members should be defined inside the class block, but outside the methods.
Move all your "public" lines above the "void Start()" line.
As Flux and Peter mentioned, access modifiers like public and private can only be applied to Class level members. This means members that are at the same level as your functions.
Depending on whether you want to use the speed variable everywhere in this class or just in one function, will dictate what approach you use.
Now based on my experience in game programming, speed is something you would want as a class level variable, and you would want to be able to access that variable from other objects as well.
So I will also point out that you should use encapsulation for a very good reason. If at some point you want to change how the speed variable is used, or how it is calculated, then encapsulation will allow you to change the implementation of that "property" without necessarily having to modify the code in other objects that use it.
e.g.
public class move : MonoBehaviour
{
private float speed = 3.0f;
public void SetSpeed(float newSpeed)
{
speed = newSpeed;
}
public float GetSpeed()
{
return speed;
}
// Use this for initialization
void Start ()
{
...
}
// Update is called once per frame
void Update ()
{
...
}
}