- Home /
Cycle Through GameObjects in Array Issue.
I am new to Unity. I am trying to create a walk-through application from 360 photos. Essentially it is supposed to work as below:
I modified a script I found online, but fear I am not setting the array up properly. Currently when I click the forward arrow I get a nice big fat crash (frozen with no other info). Here is the script. Any help appreciated. Forward Script:
public class ArrayNAV : MonoBehaviour
{
public GameObject[] Spheres;
int currentIndex = 0;
GameObject currentObject;
void Start()
{
//Instantiate initial object
currentObject = Instantiate(Spheres[currentIndex]);
}
public void Next()
{
Destroy(currentObject);
currentIndex++;
currentIndex = currentIndex >= Spheres.Length ? 0 : currentIndex;
currentObject = Instantiate(Spheres[currentIndex]);
}
}
What are those arrows? sprites?
How do you detect clicks on them?
How is
Next
function called?Where is the
ArrayNAV
script attached?
Arrows are simply sprites (with trigger collider)
A simple event trigger is used to detect clicks on the arrows
The next function is called through the event trigger
The ArrayNAV script is attached to an empty gameobject.
$$anonymous$$uch thanks for the response.
In the event of your event trigger where you call Next
, is there any other function you call which could cause a problem? (like an infinite loop mentionned by JedBeryll)
Does Unity completely freeze and you have to kill it using task manager?
Currently when I click the forward arrow
Are you inferring the other arrow works as intended?
Sorry no. I assumed after I sorted out the Next function issue I could create a similar script to the "back arrow"but have it use:
public void Previous()
{
Destroy(currentObject);
currentIndex--;
currentIndex = currentIndex < 0 ? Spheres.Length : currentIndex;
currentObject = Instantiate(Spheres[currentIndex]);
}
Noob question: Does the event trigger component check for a raycast hit, like:
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
I am using a mouselook script that allows the user to look around and click the arrow with a UI reticle.
No, the ray itself doesn't do anything. You have to use it in another method like Physics.Raycast. Even then it won't help, because the arrow doesn't have a collider. You could just use a Button component which surely works... By the way, is there an EventSystem in the scene?
Gotcha. There was no EventSystem. The arrow is not part of the Canvas, but it has a box collider set as a trigger. I think this is my problem. The previous method (loading a level vs instantiating a sphere prefab) I used a simple raycaster script to call a level load:
using UnityEngine;
public class RayCast1 : $$anonymous$$onoBehaviour
{
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider != null)
{
Application.LoadLevel("TrailNAV1");
}
}
}
}
}
Without an ES managing input (or a script such as the one above) when I click the mouse the trigger does not receive a hit, correct? So there would be nothing to report (through console). This is essentially why nothing is happening, right?
Answer by JedBeryll · Aug 28, 2019 at 04:30 PM
Most of the time, a freeze without error means that you've created an infinite loop. Also currentIndex > Spheres.Length
should be currentIndex >= Spheres.Length
because if you have an array with 3 elements, index 3 will result in an IndexOutOfRangeException.
I corrected the script based on your comment. Updating it here in a few. I'm not getting the freeze after a system reboot, but now I think the event trigger is somehow broken. When I click on the arrow it does nothing. I'll check for any other potentially conflicting scripts.
Please show an image of the event trigger and the hierarchy window as well.
Suddenly having issues with uploading images here. Gimme a few. I can't get anything to upload now. ;(
That line is bringing currentIndex
back to 0 if it's equal to Spheres.Length
so there shouldn't be a loop happening.
I know that. That's why I started my second sentence with "also" because the 2 are not related.
Your answer
Follow this Question
Related Questions
Can´t instantiate objects in list correctly 1 Answer
How to put gameObjects to the list? 4 Answers
C# How to Find the Height of an Instantiated GameObject in an Array? 1 Answer
Prefabs instantiated from an array are keeping their public int value 1 Answer
Finding the Sum of Values of Multiple GameObjects in an Array + Variable Sized arrays 0 Answers