Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need help with item spawning script.

Discussion in 'Scripting' started by Aaron212, Jun 18, 2016.

  1. Aaron212

    Aaron212

    Joined:
    Jun 18, 2016
    Posts:
    6
    So I am very new to C# so I like to use other people scripts and then learn from them. I am trying to use this script I found in a youtube channel but I am getting numerous errors and I am not sure what is wrong, the list was about 3 times longer but I fixed those however I can't seem to fix them.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class LootManager : MonoBehaviour
    7. {
    8.     public List<Loot> loot = new List<Loot> ();
    9.     public List<GameObject> spawnPoints = new List<GameObject> ();
    10.  
    11.     void Start()
    12.     {
    13.         for(int i = 0; i < Loot.Count; i++)
    14.         {
    15.             if(Random.value * 100 < Loot.spawnChance)
    16.             {
    17.                 Instantiate(Loot.lootObject, spawnPoints(Random.Range(0,spawnPoints.Count)).transform.position,Quaternion.identity);
    18.                 Debug.Log(Loot.lootObject.name + "Has spawned in the location: " + Loot.lootObject.transform.position);
    19.                 Debug.Log("Spawn chance is:" + Loot.spawnChance + "*");
    20.             }
    21.         }
    22.     }
    23. }
    24. [System.Serializable]
    25. public class Loot
    26. {
    27.     public GameObject lootObject;
    28.     public float spawnChance;
    29. }
    30.  


     
    Last edited: Jun 18, 2016
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
  3. Aaron212

    Aaron212

    Joined:
    Jun 18, 2016
    Posts:
    6
  4. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    Variables are case sensitive. You're using Loot when you should be using loot. Directly accessing an element in a list or array requires you to specify the index surrounded by square brackets.

    Code (CSharp):
    1. public class LootManager : MonoBehaviour
    2. {
    3.     public List<Loot> loot = new List<Loot>();
    4.     public List<GameObject> spawnPoints = new List<GameObject>();
    5.  
    6.     void Start()
    7.     {
    8.         for (int i = 0; i < loot.Count; i++)
    9.         {
    10.             if (Random.value * 100 < loot[i].spawnChance)
    11.             {
    12.                 Instantiate(loot[i].lootObject, spawnPoints[Random.Range(0, spawnPoints.Count)].transform.position, Quaternion.identity);
    13.                 Debug.Log(loot[i].lootObject.name + "Has spawned in the location: " + loot[i].lootObject.transform.position);
    14.                 Debug.Log("Spawn chance is:" + loot[i].spawnChance + "*");
    15.             }
    16.         }
    17.     }
    18. }
     
    Last edited: Jun 18, 2016