'Unity C# error: (12,47): error CS1503: Argument 2: cannot convert from 'System.Collections.Generic.List<UnityEngine.GameObject>' to 'float'
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 }
Solution 1:[1]
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
Solution 2:[2]
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);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |