if statement not executing in c# unity [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to make a character move a certain distance then change direction when they travel a certain distance... the logic here in my code might be wrong I still have to work on that that's not the issue, the problem is my if statement doesn't execute
public class EnemyControl : MonoBehaviour
{
private int xMoveDirection=-1;
private float x;
void Start()
{
x=gameObject.transform.position.x;
}
void Update()
{
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(xMoveDirection,0);
if(x==0.00){
Debug.Log("helloimhere");
xMoveDirection=0;
x=x+1;
}

Any direct comparisons against floating point values will most likely fail, you're better off defining a range of values that are "close enough", for example Math.Abs(x) < 0.0001.

Related

CS1612 Cannot modify the return vaue because it is not a variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
private void CmdPositionRel_Click(object sender, EventArgs e)
{
CmdTest.Location = new Point(
CmdTest.Location.X = + 20, CmdTest.Location.Y);
What I am trying to achive is that by pressing the button CmdPosition the button CmdTest goes 20 pixels in the positive direction of X.
According to the bock I am learning from this code is right...
Problem : CS1612 Cannot modify the return value of control.Location because it is not a variable
In all the other threads the answer was to add "new Point". I have that but still the problem appears.
Sorry I am a complete beginner, in programming and on StackOverflow. Hope you can help me. Thanks
you are trying to make assignment to CmdTest.Location. check my fix
CmdTest.Location = new Point(
CmdTest.Location.X + 20, CmdTest.Location.Y);

Convert.ToInt32("example") Collisions? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Will the C# function Convert.ToInt32(text) convert different words to the same int? Any links or pushes in the right direction are appreciated!
P.S.
What about anagrams?
No, Convert.ToInt32(text) will just try to parse your text to an int, like:
Convert.ToInt32("032") will return 32 as int but
Convert.ToInt32("Brian") will throw an exception.
I assume that you want to have some kind of hashing, when you say "different words to the same int".
Try GetHashCode(). It will return the same value if you call it multiple times with the same value, for example:
"Brian".GetHashCode() will always return 1635321435

How to move an object to random coordinates in Unity? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'am reviewing the code for the project.
What I want to do is the following picture.
I have some questions about the project.
As a beginner, I will not have enough explanation,
but I would appreciate it if you could give it a look.
Here are the questions:
How do I create an object called small_star as indefinitely, as shown in the picture?
Currently, prefab x, y coordinate values ​​are centered on the scene.
How do I move an object with random coordinates like a photo? And when I look at the code, I use Mathf.Cos and Mathf.Sin. What effect does it have?
I think I should implement it, but it is too much for me to do coding.
I'm a beginner. I would really appreciate it if you could give me a specific explanation.
You dont provide much information, but this may work
public GameObject small_star;
public float xMinBoundary;
public float yMinBoundary;
public float xMaxBoundary;
public float yMaxBoundary;
void MoveSpaceShip(){
float randX = Random.Range (xMinBoundary, xMaxBoundary);
float randY = Random.Range (yMinBoundary, yMaxBoundary);
Vector2 target = new Vector2 (randX,randY)
//Option 1
//small_star.transform.Translate(target * Time.deltaTime);
//Option 2
small_star.transform.position(target)
}
You will need to adapt to your needs

How to convert Vector3 to Quaternion? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Trying to figure out how to convert a Vector3 to a Quaternion. Also will it be OK if I need to constantly update said value?
You can quite easily convert a Vector3 to a quaternion by using, for example, this:
Quaternion quaternion = Quaternion.Euler(v.x, v.y, v.z);
So, for example, that's
Quaternion rot = Quaternion.Euler(V3.x, V3.y, V3.z);
That should do it!

Back to the beginning of the code with several events? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm a beginner in C# and I make a quiz. My problem it is that when a question is asked that should make a loop so that the second question can be asked. I had the idea to use goto or return to turn over to the beginning but I have several events of clicks.
Here is an example:
private void Event1
{
//do something..
}
private void Event2
{
//do other things
}
private void Event3
{
//Here I want to return to the event 1 to make a loop
}
Is that possible?
If no are there other solutions?
I assumed that you want to bulid 3 question in series, enable the next options when current question is answered correctly.
If I'm right, first of all you should enable the first button only.
When first event raised and the answer was right , disable first button, then enable next one,so on until all questions were finished.

Categories

Resources