Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Error in a script but I don't know why

Discussion in 'Scripting' started by amandine13, Apr 11, 2016.

  1. amandine13

    amandine13

    Joined:
    Apr 4, 2016
    Posts:
    27
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System;
    5. public class CreationMeteorites : MonoBehaviour {
    6.  
    7.     public GameObject MeteoritesObjet;
    8.     private float tempo1 = 0.0f;
    9.     void Start() {
    10.         InvokeRepeating ("addMeteorite", tempo1, tempo1);
    11.         tempo1 += Time.deltaTime;
    12.     }
    13.     void addMeteorite {
    14.         tr = GetComponent<"Transform">();
    15.         rd = GetComponent<"Renderer">();
    16.         x1 = tr.position.x - rd.bounds.size.x/2;
    17.         x2 = tr.position.x + rd.bounds.size.x/2;
    18.         spawnPoint = Vector2(Random.Range(x1,x2),tr.position.y);
    19.         Instantiate(Meteorites,spawnPoint,Quaternion.identity);
    20.     }
    21.  
    22. }
    23.  
    Hello, I've got a problem with this script but I don't find the solution.. Is there someone who know how to solve it ?
    Thanks
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    what problem?

    at a quick glance you shouldn't be using "" in the types for the generic functions...
     
  3. amandine13

    amandine13

    Joined:
    Apr 4, 2016
    Posts:
    27
    At line 14 I've got this message "get or set accessor expected"
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    then it would indeed appear to be the "" causing the problem.

    Generic functions take a type in the < > , so something like

    Code (csharp):
    1.  
    2. GetComponent<Transform>();
    3.  
     
    amandine13 likes this.
  5. amandine13

    amandine13

    Joined:
    Apr 4, 2016
    Posts:
    27
    I've tried without but the problem still the same..
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ok, second glance...

    there is no "()" after the addMeteorite function name (that's causing the error you pasted above).
    also, you should then run into issues because "tr" and "rd" don't have any type declaration, nor does x1 or x2 for that matter.

    You need to declare the type of variables, even functions that don't take any parameters need the () to define them as functions.

    Code (csharp):
    1.  
    2. void addMeteorite()
    3. {
    4.         Transform tr = GetComponent<Transform>();
    5.         Renderer rd = GetComponent<Renderer>();
    6.         float x1 = tr.position.x - rd.bounds.size.x/2;
    7.         float x2 = tr.position.x + rd.bounds.size.x/2;
    8.         //... etc.
    9.  

    you might also find this interesting: http://www.dofactory.com/reference/csharp-coding-standards
     
    amandine13 likes this.
  7. amandine13

    amandine13

    Joined:
    Apr 4, 2016
    Posts:
    27
    It works, thanks very much