-2

I'm new to programming in general.

With another Script i store in the list "spawnPointC" all gameobjects called spawnPointC that appear from spawning other prefabs.

I want to pick a random GameObject from that list, and store it's position to spawn another object at the same position later.

I've tried some other things, but i don't know what i'm doing.

How would you do it?

1  using System.Collections;
2  using System.Collections.Generic;
3  using UnityEngine;
4
5  public class CspList : MonoBehaviour
6  {
7    public List<GameObject> spawnPointsC;
8    [SerializeField] private GameObject goal;
9 
10   void Start()
11   {
12       GameObject spawnIndex = Random.Range(0, spawnPointsC);
13 
14      float pointX = spawnIndex.transform.position.x;
15      float pointY = spawnIndex.transform.position.y;
16      float pointZ = spawnIndex.transform.position.z;
17
18      Vector3 pointSpawn = new Vector3(pointX, pointY, pointZ);
19      Instantiate(goal, pointSpawn, Quaternion.identity);
20   }
21 }
LSX Y
  • 3
  • 3
  • 1
    Well you probably rather wanted `GameObject spawnIndex = Random.Range(0, spawnPointsC.Count);` ... a `List` is not an `int` or `float` ^^ I would categorize this as a typo ;) – derHugo May 12 '22 at 15:49
  • Thank you for the reply, i tried that and the error i get with that is: – LSX Y May 12 '22 at 15:52
  • (12,31): error CS0029: Cannot implicitly convert type 'int' to 'UnityEngine.GameObject' – LSX Y May 12 '22 at 15:52
  • show us your edited code – Szpur May 12 '22 at 15:54
  • oh i know. Now you tried to use integer as a game object here: `float pointX = spawnIndex.transform.position.x;` – Szpur May 12 '22 at 15:55
  • How do i show a screenshot in the comments? Ahm, is that right, Szpur? – LSX Y May 12 '22 at 16:01
  • I edited my original post down below and added proper solution, at least in my opinion :D – Szpur May 12 '22 at 16:02
  • oh sorry ^^ copy paste mistake: `int spawnIndex = Random.Range(0, spawnPointsC.Count);` ... a `GameObject` is not an index either – derHugo May 12 '22 at 16:02

2 Answers2

0

Ok so as said you simply wanted to pass in

spawnPointsC.Count

since you want the amount of items, not the entire list instance.

Further for an index it makes no sense to have the type GameObject. You want an int or simply var since the compiler already "knows" what is returned by Random.Range anyway, there is no need to explicitly tell it that it is an int

var spawnIndex = Random.Range(0, spawnPointsC.Count);

And as a side note a Vector3 is a struct. You can shorten your code to simply

void Start()
{
    // this is an int!
    var spawnIndex = Random.Range(0, spawnPointsC.Count);
    //                          this returns the according GameObject item from the list
    //                          | 
    //                          |                   Now you access the Transform and it's position only ONCE
    //                          |                   |
    //                          v                   v
    var randomSpawnPoint = spawnPointsC[spawnIndex].transform.position;
      
    // Since Vector3 is a struct it is a COPY by VALUE
    // There is no need for using new, just pass it on
    Instantiate(goal, randomSpawnPoint, Quaternion.identity);
}

this is also way more efficient than repeatedly accessing the array and/or properties of the object

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • I literally copy-pasted and can now run the code, only get this error: ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index – LSX Y May 12 '22 at 16:18
  • if that so, maybe your list is empty? – Szpur May 12 '22 at 16:23
  • I was thinking that i might have to delay this script as the list is created when i start the game, (because of spawning prefabs at Start). So the list is empty in the Start. – LSX Y May 12 '22 at 16:28
  • hmm after the Delay i still get the error, and the list isn't empty anymore. – LSX Y May 12 '22 at 17:52
  • I GOT IT, both codes you provided as solution are correct, i wasn't referencing the List correctly in the second code (as i separated them due to the delay) but that wasn't needed. – LSX Y May 12 '22 at 18:01
-1

The errors tells you that you trying to list of GameObjects as float and its happening here:

GameObject spawnIndex = Random.Range(0, spawnPointsC.Count);

I'm not exactly sure why you would try to do, but maybe try to use spawnPointsC.Count as @derHugo mention.

Try this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CspList : MonoBehaviour
{
   public List<GameObject> spawnPointsC;
   [SerializeField] private GameObject goal;

   void Start()
   {
      int spawnIndex = Random.Range(0, spawnPointsC.Count);
     
      float pointX = spawnPointsC[spawnIndex].transform.position.x;
      float pointY = spawnPointsC[spawnIndex].transform.position.y;
      float pointZ = spawnPointsC[spawnIndex].transform.position.z;
     
      Vector3 pointSpawn = new Vector3(pointX, pointY, pointZ);
      Instantiate(goal, pointSpawn, Quaternion.identity);
  }
}
Szpur
  • 109
  • 1
  • 7
  • So i tried this:``` float pointX = spawnPointsC[spawnIndex -1].transform.position.x; float pointY = spawnPointsC[spawnIndex -1].transform.position.y; float pointZ = spawnPointsC[spawnIndex -1].transform.position.z; ``` (16,35): error CS0019: Operator '-' cannot be applied to operands of type 'GameObject' and 'int' – LSX Y May 12 '22 at 16:03
  • fixed again, refresh comment :D – Szpur May 12 '22 at 16:04
  • @LSXY fix erros one by one from top to bottom – derHugo May 12 '22 at 16:04
  • 1
    @Szpur your comment on `-1` is wrong though .. the upper bound of `Random.Range(int,int)` is **exclusive** .. it would even produce an error now if the random happens to return `0` you try to access index `-1` ;) – derHugo May 12 '22 at 16:04
  • thats happed when you try to help too harshly XD – Szpur May 12 '22 at 16:06
  • ok, so i see you changed the data type of GameObject to int, i really think i need it as a GameObject, i will try some other things you said too, brb. – LSX Y May 12 '22 at 16:16
  • also i changed lines 14,15,16 -> use list with index you generated – Szpur May 12 '22 at 16:23