Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

error CS1503

Discussion in 'Scripting' started by Swissforce, May 26, 2019.

  1. Swissforce

    Swissforce

    Joined:
    Jan 6, 2019
    Posts:
    3
    I have this error but I couldn't find a fitting answer on how I should correct it:

    error CS1503: Argument 1: cannot convert from 'UnityEngine.GameObject[]' to 'UnityEngine.Object'

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Endgame1 : MonoBehaviour
    6. {
    7.     public Transform teleportTarget;
    8.     public GameObject thePlayer;
    9.     public GameObject respawnPrefab;
    10.     public GameObject[] respawn;
    11.     void Update()
    12.     {
    13.         respawn = GameObject.FindGameObjectsWithTag("Target1");
    14.         if (respawn.Length == 0)
    15.         {
    16.             thePlayer.transform.position = teleportTarget.transform.position;
    17.             Instantiate(respawn, new Vector3(11.51f, 3, 10.21f), Quaternion.Euler(0, 180, 0));
    18.             Instantiate(respawn, new Vector3(11.51f, 3, 6.32f), Quaternion.Euler(0, 180, 0));
    19.             Instantiate(respawn, new Vector3(11.51f, 3, 1.54f), Quaternion.Euler(0, 180, 0));
    20.             Instantiate(respawn, new Vector3(11.51f, 3, 1.54f), Quaternion.Euler(0, 180, 0));
    21.         }
    22.     }
    23. }
     
  2. DaDonik

    DaDonik

    Joined:
    Jun 17, 2013
    Posts:
    140
    respawn is an array. Instantiate takes a single GameObject as it's first parameter, not an array.

    You probably intended that to be: Instantiate(respawnPrefab, ...

    In the future please check the documentation of Unity's built-in functions. Also include the whole error message, it usually tells you the exact line where the problem occurs.
     
  3. Swissforce

    Swissforce

    Joined:
    Jan 6, 2019
    Posts:
    3

    You have to know that my game is about shooting on targets and if all the targets tagged are destroyed they should respawn again .but I dont know how I can check for multiple, if you say gameobject should be 1.
    You're right that's my error but I have no other option or do I?

    Im sorry im not very good at programming and im admiring the skills of all of you skilled people out there.
    I hope you understand my problem
     
  4. DaDonik

    DaDonik

    Joined:
    Jun 17, 2013
    Posts:
    140
    Assuming you want to place the targets in the scene, you would need something like that:
    1) Store references to your targets in some datastructure, so you can check if they are still there, or not.
    2) Store the position and roation of your targets, so you can recreate them where they were before you destroyed them.
    3) Figure out how you can efficiently check if the targets are all destroyed. (hint: NOT via FindGameObjectsWithTag each Update)
    4) Figure out how to respawn a bunch of targets.

    That would be 4 relatively seperate steps you can then try to figure out one by one. This is where the relatively good Unity documentation and the vast amount of 'googleable' resources come in handy.

    Here is a solution i came up with:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Endgame1 : MonoBehaviour
    4. {
    5.     public Transform teleportTarget;
    6.     public GameObject thePlayer;
    7.     public GameObject respawnPrefab;
    8.     private GameObject[] respawnObjects;
    9.     private Vector3[] respawnPositions;
    10.     private Quaternion[] respawnRotations;
    11.  
    12.     private bool HasRemainingTargets()
    13.     {
    14.         for (int i = 0; i < respawnObjects.Length; i++)
    15.         {
    16.             if (respawnObjects[i] != null) { return true; }
    17.         }
    18.  
    19.         return false;
    20.     }
    21.  
    22.     private void RespawnTargets()
    23.     {
    24.         for (int i = 0; i < respawnObjects.Length; i++)
    25.         {
    26.             respawnObjects[i] = Instantiate(respawnPrefab, respawnPositions[i], respawnRotations[i]);
    27.         }
    28.     }
    29.  
    30.     private void TeleportPlayer()
    31.     {
    32.         thePlayer.transform.position = teleportTarget.transform.position;
    33.     }
    34.  
    35.     void Start()
    36.     {
    37.         respawnObjects = GameObject.FindGameObjectsWithTag("Target1");
    38.         if (respawnObjects.Length > 0)
    39.         {
    40.             respawnPositions = new Vector3[respawnObjects.Length];
    41.             respawnRotations = new Quaternion[respawnObjects.Length];
    42.             for (int i = 0; i < respawnObjects.Length; i++)
    43.             {
    44.                 respawnPositions[i] = respawnObjects[i].transform.position;
    45.                 respawnRotations[i] = respawnObjects[i].transform.rotation;
    46.             }
    47.         }
    48.     }
    49.  
    50.     void Update()
    51.     {
    52.         if (HasRemainingTargets() == false)
    53.         {
    54.             TeleportPlayer();
    55.             RespawnTargets();
    56.         }
    57.     }
    58. }
    59.  
    This is by no means the only or the best solution, but it does what you want...i guess.
    Should be rather self explanatory. If not, you know you have some things to learn... :D
     
  5. Swissforce

    Swissforce

    Joined:
    Jan 6, 2019
    Posts:
    3
    Thank you for the code, but the problem is, that I can respawn them because the position is deleted. Still thank you very much