Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Cant find the error!

Discussion in 'Scripting' started by rmele09, Feb 1, 2013.

  1. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    712
    Hey guys I can't seem to figure out what is wrong with this simple script. I am getting an error that I need to insert a semi colon and there is one...It's line 14 that shows the error.

    Code (csharp):
    1. function Update () {
    2.  
    3. var hit : RaycastHit;
    4. var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    5.  
    6. if (Input.GetMouseButtonDown(0))
    7. {
    8. if (Physics.Raycast(ray, hit, 100))
    9.  
    10. {
    11.    
    12.     if (hit.transform.tag == "Enemy Sphere")
    13.     {
    14.         var posSphere : Vector3(Random.Range(-15 , 15), Random.Range(-3, 3), 0);
    15.         hit.transform.position = posSphere;
    16.         print ("You hit Sphere");
    17.    
    18.     }
    19.    
    20.     if (hit.transform.tag == "Enemy Cube")
    21.     {
    22.         print("You hit Cube");
    23.     }
    24.  
    25. }
    26. }
    27. }
     
  2. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    712
    Think I found it, needs to look like this:

    var posSphere : Vector3 = Vector3(Random.Range(-15 , 15), Random.Range(-3, 3), 0);

    I am following a tutorial and the guy's code looks like mine posted above, the incorrect way. Why does it work for him in the video? Does Unity change the way these functions work from time to time? Maybe the guy in the tutorial is using an older version?
     
  3. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    That wouldn't work anywhere, he may have used
    Code (csharp):
    1.  
    2. var posSphere = Vector3 (...);
    3.  
    though.
     
  4. G-Modifications

    G-Modifications

    Joined:
    Apr 6, 2012
    Posts:
    64
    rmele09's solution is also good. It's actually even faster.
     
  5. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    712
    oh yea I see what you mean. Thanks guys for the responses, seems to be working now.