#I need help but i will put it in a
1 messages · Page 1 of 1 (latest)
hey i keep having this error:
IndexOutOfRangeException: Index was outside the bounds of the array.
Portal.OnCollide (UnityEngine.Collider2D coll) (at Assets/scripts/Portal.cs:12)
Collidable.Update () (at Assets/scripts/Collidable.cs:25)
here is my code:
**Portal.cs
public class Portal : Collidable
{
public string[] sceneNames;
protected override void OnCollide(Collider2D coll)
{
if (coll.name == "Player")
{
// teleport player
string sceneName = sceneNames[Random.Range(0, sceneNames.Length)];
UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName);
}
}
}```
**Collidable.cs**
```using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collidable : MonoBehaviour
{
public ContactFilter2D filter;
private BoxCollider2D boxCollider;
private Collider2D[] hits = new Collider2D[10];
protected virtual void Start()
{
boxCollider = GetComponent<BoxCollider2D>();
}
protected virtual void Update()
{
// collision stuff
boxCollider.OverlapCollider(filter, hits);
for (int i = 0; i < hits.Length; i++)
{
if (hits[i] == null)
continue;
OnCollide(hits[i]);
hits[i] = null;
}
}
protected virtual void OnCollide(Collider2D coll)
{
Debug.Log(coll.name);
}
}```
i followed this tutorial:
https://www.youtube.com/watch?v=b8YUfee_pzc&t=3136s
anyone that is able to help?
This is a full release of an Top Down RPG course made in Unity 2017
0:00:00 - Intro
0:01:31 - Setting up
0:16:03 - Moving and Manual Collision Detection
1:03:06 - Tilemap and Designing Dungeon
1:40:31 - Interactive Objects and Inheritance
2:15:37 - Saving Game's State
2:41:31 - Floating Text System
3:11:11 - Top Down Combat System
4:13:32 - Ani...
You will get that error if your sceneNames array has size of 0
alright so in line 12 collum 56/57 the 0 in there has to be removed?
no, in your inspector you need to populate the array
the code is fine
alright i got it like this do i just write the scene name in the element?
add all the scene names you wish to randomly choose from there, yes
Scene 'Dungeon1' couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.
To add a scene to the build settings use the menu File->Build Settings...
UnityEngine.SceneManagement.SceneManager:LoadScene (string)
Portal:OnCollide (UnityEngine.Collider2D) (at Assets/scripts/Portal.cs:13)
Collidable:Update () (at Assets/scripts/Collidable.cs:25)
now gives me this error ^^
oh wait
i think i know how to fix this
yep it tells you