Hello everyone, i would like to preface that i am new to unity. I am starting to develop a game. The first step i wanted to take was to have backgrounds randomly put in. Already made and in the resources folder, directory being /resources/Backgrounds. I wrote a script that i will put on the bottom. I keep getting an error code. I have also attached the script to the Main Camera in the scene. The error code i keep getting is ‘The referenced script on this Behaviour (Game Object ‘BackgroundCamera’) is missing!’. What do i need to do to fix this. Also if my script looks bad or can be improved please let me know i would love all the constructive criticism i can get.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class RandomFromList : MonoBehaviour
{
public List roomList;
public GameObject[ ] roomListArray;
I would also like to add that the backgrounds are .png files. The scene also only has the camera so far. I wanted to get this working as my first step.
That error can show up if you move the script files to a different folder outside of Unity. When you do that I think Unity will pick up the script as something new and assign it a new hash which breaks any existing references to it.
Does the error go away if you remove the component and add it again?
instead of creating an array and then a list from the array. But that isn’t your error. As @Waffleslug suggested. First try removing and adding the script again. If you still have an error, make sure you properly saved your script and you also don’t have console errors.
Update: I have fixed the error and it ‘runs’ now. Though it doesn’t put the background on the screen. What am i missing or doing wrong in my code? It still has a warning symbol beneath the script saying there are compile errors, though it does not show up in console when running.
Right now you are creating an array and then converting it to a list. You now have an array and a list. Are you using both? If not, you should pick one and go with it. Either the array or the list then you don’t have one of them just sitting there serving no purpose. Think of it as creating two shopping list and then going to the store with both, but only using one of them.
What are you loading? What is a “background” gameobject that you’re populating your list with?
Hmm…if they are images, I don’t think your setup will work. You’ll have to load them as textures or sprites. I’ve never done a resource.loadall for images.
I don’t believe they will work as gameobjects. Do you even have any values when you debug.log your list count? Normally for something like this I’d probably just have a public away where I drop the images into it and then randomly pick from that array to apply a background.
I apologize looking at it today i realize that the Texture Type is Sprite (2D and UI). I also keep getting the error script is missing after every time i run playmode. Deleting and copying and pasting works though it is getting a bit annoying doing it every time i hit play. I also do not get anything from the debug.log and still getting that compiling error. I am not sure what all i am doing wrong.