Search Unity

Problem with code

Discussion in 'Scripting' started by MG, Jun 30, 2015.

  1. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    I have this code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class spawn : MonoBehaviour {
    5.  
    6.     public Transform[] spawnPoint;
    7.     public GameObject tnt;
    8.     public int Level = 1;
    9.     public int a = 0;
    10.     public int score = 0;
    11.  
    12.     public float offsetY = 40;
    13.     public float sizeX = 100;
    14.     public float sizeY = 40;
    15.     public float delay;
    16.  
    17.     public Transform Count3;
    18.     public Transform Count2;
    19.     public Transform Count1;
    20.     public Transform CountGo;
    21.     public Transform CountSpawn;
    22.  
    23.     // Use this for initialization
    24.     void Start () {
    25.         Time.timeScale = 0;
    26.         InvokeRepeating ("SpawnTNT", 4, delay);
    27.     }
    28.    
    29.  
    30.     void Update () {
    31.    
    32.         if (a > 10) {
    33.             if(delay > 0.3F){
    34.             delay *= 0.9F;
    35.             a=0;
    36.             Level++;
    37.            
    38.             }
    39.         }
    40.     }
    41.  
    42.     void SpawnTNT ()
    43.     {
    44.         int i = Random.Range (0, 5);
    45.         Instantiate (tnt, spawnPoint [i].position, Quaternion.identity);
    46.         a++;
    47.     }
    48.  
    49.     void OnGUI()
    50.     {
    51.         GUI.Box (new Rect (Screen.width / 2 - sizeX/2, offsetY, sizeX, sizeY), "Score: "+score);
    52.     }
    53.  
    54.     void addScore ()
    55.  
    56.     {
    57.         score++;
    58.     }
    59.  
    60.     void Go ()
    61.     {
    62.         Time.timeScale = 1;
    63.         Invoke ("SpawnCount3", 0);
    64.     }
    65.  
    66.     void SpawnCount3()
    67.     {
    68.         Instantiate (Count3, CountSpawn, Quaternion.identity);
    69.     }
    70. }
    71.  
    And im getting these errors

    Code (CSharp):
    1. Assets/scripts/spawn.cs(68,17): error CS1502: The best overloaded method match for `UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments
    2.  
    3.  
    4. Assets/scripts/spawn.cs(68,17): error CS1503: Argument `#2' cannot convert `UnityEngine.Transform' expression to type `UnityEngine.Vector3'
    5.  
    6.  
     
  2. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422

    CountSpawn is a Transform, you need a position (Vector3).

    Code (csharp):
    1. Instantiate(Count3, CountSpawn.position, Quaternion.identity)

    Edit: This was misleading/incorrect, so struck out.
    Note, you're instantiating a transform there, is that what you want? (Count3 is a transform) did you want to instantiate a GameObject or a Transform? I expect you want a GameObject.
     
    Last edited: Jun 30, 2015
    LaneFox likes this.
  3. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,529
    Should still work as a Transform iirc.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
  5. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    I'm pretty sure that @LaneFox is correct. The two overloads both take Object, which I assume both Transform and GameObject both inherit from.

    From that link (emphasis mine):

    If you are cloning a GameObject then you can also optionally specify its position and rotation (these will default to Vector3.zero and Quaternion.identity respectively). If you are cloning a Component then the GameObject it is attached to will also be cloned, again with an optional position and rotation.

    As such, Transform is a Component (inherits from) and therefore it will clone the attached GameObject also (but I assume it still returns (correctly) a reference to the Transform and not the cloned GameObject, but the GameObject can be got from transform.gameObject, etc).

    I've amended my previous post to remove my misleading/incorrect statement.
     
    LaneFox likes this.