Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Properpeanut · Apr 14, 2017 at 10:40 AM · instantiate prefabarray of gameobjects

Loot System that load items from resource folder to array

Hello everyone.

I'm fiddling with a loot system kinda like Dayz, but dumbed down. In short, I have an empty gameobject called "SpawnFood" as a prefab and I want to be able to just place it around my map and make it spawn a random item from an array. I want it to spawn an item when the player gets within a certain distance and only if there is not an item there already. Also I'm trying to make it despawn the item after a certain time if the player does not pick it up.

This is what I got so far:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LootManager : MonoBehaviour
 {
 
     public GameObject[] foodItem;
     public Transform spawnPoint;
     public bool isSpawnable = true;
     private GameObject player;
 
     public float distance;
     public float respawnTimer = 1.0f;
     public float despawnTimer = 1.0f;
 
 
     private void Start()
     {
         player = GameObject.FindGameObjectWithTag("Player");
         foodItem = Resources.LoadAll("Fooditems") as GameObject[];
     }
 
     void Update()
     {
         int foodItem_num = Random.Range(0, foodItem.Length);
         distance = Vector3.Distance(transform.position, player.transform.position);
         
         if (distance <= 5.0f && isSpawnable == true && respawnTimer <= 0)
         { 
             Instantiate(foodItem[foodItem_num], spawnPoint.position, spawnPoint.rotation);
             foodItem[foodItem_num].transform.SetParent(spawnPoint);
             Debug.Log("Spawning Item");
             isSpawnable = false;
         }
         else
         {
             return;
         }
 
         if (isSpawnable == false)
         {
             despawnTimer -= Time.time;
             if (despawnTimer <= 0.0f)
             {
                 if (transform.childCount > 0)
                 {
                     Transform child = transform.GetChild(0);
                     child.parent = null;
                     Destroy(child.gameObject);
                 }
             }
         }
     }
 }
 

I get 2 errors with this code. An "Array Index is out of range", which points me to this line:

             foodItem[foodItem_num].transform.SetParent(spawnPoint);

and an "Object reference not set to an instance of an object", which points me to this;

             Instantiate(foodItem[foodItem_num], spawnPoint.position, spawnPoint.rotation);


Can anyone tell me what I'm doing wrong? When I press play, the items that I've put in the fooditem array in the inspector disappears and its size is set to 0.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Laseph · Apr 14, 2017 at 12:11 PM

You are setting foodItem array in the start method, could it be that there's something wrong with loading the resources, the wrong path or something, and that's why foodItem has no elements. That would also explain your errors. It's out of bounds because the array length is zero and there are no objects in the array.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Properpeanut · Apr 14, 2017 at 12:42 PM 0
Share

I checked the path. The prefabs are located in an "FoodItems" folder within the Resource folder. Do I need a "/" before or after the foldername?

avatar image Laseph Properpeanut · Apr 14, 2017 at 01:08 PM 0
Share

No, that should be good. $$anonymous$$aybe try this ins$$anonymous$$d in start.

 foodItem = Resources.LoadAll("Fooditems").Cast<GameObject>().ToArray();

You will also need to add this namespace for it to work.

  using System.Linq;

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

99 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to keep gameobject by array synclist type 1 Answer

How do you spawn a gameobject based on last object in array and limit that within a range? 1 Answer

Getting older elements of an Array/List and looping to the latest elements if index is less than zero 1 Answer

Instantiating once in an update 1 Answer

Instantiate and launch prefab that's overlapping player? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges