public class WindMov : MonoBehaviour
{
public GameObject wind;
public float mSpeed = 5f;
public Transform currentPosition;
public Transform[] points;
public int pointSelection = 0;
public bool forward = true;
// Start is called before the first frame update
void Start()
{
currentPosition = points[pointSelection];
}
private void Update()
{
if (wind.transform.position == currentPosition.position)
{
if (forward == true)
{
pointSelection++;
}
else
{
pointSelection--;
}
if(pointSelection == points.Length && forward == true)
{
forward = false;
}else if (currentPosition.position == points[0].position && forward == false)
{
forward = true;
}
currentPosition = points[pointSelection];
}
}
void FixedUpdate()
{
wind.transform.position = Vector3.MoveTowards(wind.transform.position, currentPosition.position, Time.deltaTime * mSpeed);
}
}
hi, I’m working on this script, that seems that is working perfectly, but, it displays me an error in the console, the script its suppose to make the gameObject to go along a route and after reaching the end, goes backwards just to start again, and it totally works, but it bugs me that it displays this error, can someone enlighten me about how to fix it? it seems to display whenever it changes between true and false on the boolean variable, each time it reachs one end of the array or the other
IndexOutOfRangeException: Index was outside the bounds of the array.
WindMov.Update () (at Assets/Scripts/Elements/WindMov.cs:47)
Hello !
The error you’re seeing is caused by the fact that you’re trying to access an index that’s out of bounds of the points array. This is happening because you’re incrementing or decrementing pointSelection without checking whether it’s already at the beginning or end of the array.
To fix this, you can simply add an additional check in the Update method to make sure that pointSelection is within the bounds of the points array before you try to access it. Here’s how you can do that:
void Update()
{
if (wind.transform.position == currentPosition.position)
{
if (forward == true)
{
pointSelection++;
}
else
{
pointSelection--;
}
// Add a check to make sure pointSelection is within the bounds of the array
if (pointSelection < 0 || pointSelection >= points.Length)
{
if (forward == true)
{
forward = false;
pointSelection = points.Length - 1;
}
else
{
forward = true;
pointSelection = 0;
}
}
currentPosition = points[pointSelection];
}
}
This should fix the error you’re seeing and allow the game object to move along the route without displaying the error message.
It looks like the problem is that you are using the assignment operator (=) instead of the comparison operator (==) in the if statement that checks if wind.transform.position is equal to currentPosition.position.
The assignment operator sets the value of a variable, whereas the comparison operator compares the values of two variables and returns a boolean value (true or false) indicating whether they are equal.
To fix this issue, you should change the if statement to use the comparison operator:
if (wind.transform.position == currentPosition.position)
This will ensure that the code inside the if statement only runs if wind.transform.position is equal to currentPosition.position, rather than setting the value of currentPosition to wind.transform.position.
It might also be a good idea to add some debugging code, such as printing the values of pointSelection and currentPosition to the console, to help you understand how the code is behaving and identify any other potential issues.
I don’t think that’s it in this case… if it was then it wouldn’t compile because the result passed into the if statement would be a Vector3, not a bool.
(Perhaps I’m just not seeing the code you refer to?)
In other news, comparing Vector3 values for equality actually uses a very small Epsilon and it’s possible you can miss that, so your equals may not be ever testing true.
Floating (float) point imprecision:
Never test floating point (float) quantities for equality / inequality. Here’s why:
You are correct that the comparison operator (==) cannot be used to compare two Vector3 values, as it will produce a compile-time error. The comparison operator can only be used to compare values of primitive types (such as int, float, and bool) or values of a type that overloads the == operator to perform a custom comparison.
Since the wind.transform.position and currentPosition.position variables are of type Vector3, you cannot use the comparison operator to compare their values. Instead, you will need to compare the individual components of the Vector3 values (e.g., x, y, and z) separately.
For example, you could use the following code to check if the two Vector3 values are equal:
if (wind.transform.position.x == currentPosition.position.x &&
wind.transform.position.y == currentPosition.position.y &&
wind.transform.position.z == currentPosition.position.z)
{
// The two Vector3 values are equal
}
Alternatively, you can use the Vector3.Equals() method to compare the two Vector3 values:
if (wind.transform.position.Equals(currentPosition.position))
{
// The two Vector3 values are equal
}
Here is the full code with the modified if statement:
void Update()
{
if (wind.transform.position.Equals(currentPosition.position))
{
if (forward == true)
{
pointSelection++;
}
else
{
pointSelection--;
}
// Add a check to make sure pointSelection is within the bounds of the array
if (pointSelection < 0 || pointSelection >= points.Length)
{
if (forward == true)
{
forward = false;
pointSelection = points.Length - 1;
}
else
{
forward = true;
pointSelection = 0;
}
}
currentPosition = points[pointSelection];
}
}
Do you think this would make more sense or am I going off the board ?
thanksk for the help pals, it really dosent work, im afraid, it does an iteration and then it goes from the 0 element, and the third one of the element without touchin the elements between them uwu
This script seems to wrok fine, but i cant still rid off the console error, but this time its only after reaching the last element of the array and not the first one too
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WindiMov : MonoBehaviour
{
public GameObject wind;
public float mSpeed = 5f;
public int pointSelection = 0;
public Transform currentPosition;
public Transform[] points;
public bool forward = true;
// Start is called before the first frame update
void Start()
{
currentPosition = points[pointSelection];
}
void FixedUpdate()
{
wind.transform.position = Vector3.MoveTowards(wind.transform.position, currentPosition.position, Time.deltaTime * mSpeed);
}
void Update()
{
if (pointSelection >= points.Length && forward == true)
{
forward = false;
}
else if (pointSelection <= 0 && forward == false)
{
forward = true;
}
if (wind.transform.position == currentPosition.position)
{
if (forward == true && pointSelection < points.Length)
{
pointSelection++;
}
else if (forward == false && pointSelection > 0)
{
pointSelection--;
}
}
currentPosition = points[pointSelection];
}
}