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 ()
{
...
}
}
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 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.
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 would like to find out:
how to pass the value assigned to a variable on Start() to Update() function?
When I call the variable assigned on Start(), Debug.Log() on Update() shows null or nothing at all?
Just to give a more visual answer to the ones already stated in comments.
class MyClass
{
string hello; // Declare variable
void Start()
{
hello = "world"; // Set variable
}
void Update()
{
Debug.Log(hello); // Read variable
}
}
Curly braces { & } define a scope, a variable declared in a scope will only be accessible in itself and child scopes (functions, etc, contained in that scope). So in our case, if you declare a variable in Start(), it will not be accessible in Update() since Update() is not inside of Start()'s scope.
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 7 years ago.
Improve this question
I'm so fed up, I don't know what's going wrong here. I've got a DLGAnalysis object with a constructor with one parameter, and I'm calling the constructor from a Unit Test class.
Here's the DLGAnalysis Object:
class DLGAnalysis
{
public DLGAnalysis(string DLG)
{
_DLG = DLG;
_namespaceAnalyses = new List<NamespaceAnalysis>();
}
}
There's more to that class but it doesn't affect the problem. Here's the unit test segment:
[TestClass]
public class DLGAnalysisTests
{
// Blue line here
private DLGAnalysis dlgAnalysis;
private const string TestDLGName = "TestDLGName";
[TestInitialize]
public void TestSetup()
{
// Error here
dlgAnalysis = new DLGAnalysis(TestDLGName);
}
}
The blue line under "DLGAnalysis" says:
'AnalysisXMLParser.DLGAnalysis' is inaccessible due to its protection level
And the red line under new DLGAnalysis(TestDLGName) says:
'AnalysisXMLParser.DLGAnalysis' does not contain a constructor that takes 1 arguments
The DLGAnalysis object clearly has a public constructor with 1 argument. I have no idea what is going on here. What's the problem? Should I just delete the file and start over?
You forgot to set the protection level of your class
public class DLGAnalysis // <== public
{
Not setting a protection level defaults that class to internal.
As your unit test project is probably in a different assembly, it can't access internal classes.