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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Having Troubles with a JavaScript File for a Minecraft-Type Game

Discussion in 'Scripting' started by bigboy4006, Feb 6, 2013.

  1. bigboy4006

    bigboy4006

    Joined:
    Apr 13, 2012
    Posts:
    65
    I just started the first tutorial in YouTube Playlist on making a game like Minecraft. In the first video, the teacher takes you through scripting a file called BlockMaker.js. So far, it seems I have one little error I get from the code, and I've type the code exactly the way the guy shows.

    Here's the code:
    Code (csharp):
    1. var GrassBlock : Transform;
    2. var StoneBlock : Transform;
    3. var BlockSelected : float = 1;
    4. var Range : float = 20;
    5.  
    6. function Update () {
    7.     if (Input.GetKeyDown(KeyCode.Q));
    8.         BlockSelected -= 1;
    9.     if (Input.GetKeyDown(KeyCode.E));
    10.         BlockSelected += 1;
    11.  
    12. if (Input.GetMouseButtonDown(0) || GetMouseButtonDown(1)){
    13.         var Hit : RaycastHit;
    14.         var LookingDirection = transform.TransformDirection(Vector3.forward);
    15.        
    16.         if (Physics.Raycast(transform.position.LookingDirection, Hit, Range));
    17.         {
    18.             if (Input.GetMouseButtonDown(1))
    19.         }
    20.        
    21.         if (BlockSelected == 1)
    22.         {
    23.             var GrassBlock : Transform = instantiate(GrassBlock, Hit.collider.transform.position + Hit.normal.normalize.Quaternion.identity);
    24.             GrassBlock.tag = "GrassBlock";
    25.         }
    26. }
    The problem is this one little snippet:

    Code (csharp):
    1. if (Input.GetMouseButtonDown(1))
    I know that it shouldn't be an if statement, but I don't know JavaScript well enough to change it. This is how the guy has put it into the script, and looking at the full code shows it's nested within another if statement. Anyone have any suggestions on how to fix this problem? :confused:
     
    Last edited: Feb 6, 2013
  2. bigboy4006

    bigboy4006

    Joined:
    Apr 13, 2012
    Posts:
    65
    That code was from the first video. I just started on the second video, and I know he goes back to the script. I assume he fixes the problem, so I'll have to wait and see. I'll be watching the second video later - it's late and I need to get to bed.
     
  3. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,245
    I think the if statement below it belongs with it. So when mousebuttondown(1) instantiate new block.
     
  4. Gnatty

    Gnatty

    Joined:
    May 17, 2012
    Posts:
    77
    "If" statements do not finish with semi-colons. Remove the ";" from the three examples above.
     
  5. bigboy4006

    bigboy4006

    Joined:
    Apr 13, 2012
    Posts:
    65
    I fixed those problems, and now I have a new one.

    Code (csharp):
    1. var GrassBlock : Transform;
    2. var StoneBlock : Transform;
    3. var BlockSelected : float = 1;
    4. var Range : float = 20;
    5.  
    6. function Update ()
    7. {
    8.     if (Input.GetKeyDown(KeyCode.Q));
    9.         BlockSelected -= 1;
    10.     if (Input.GetKeyDown(KeyCode.E));
    11.         BlockSelected += 1;
    12.  
    13.     if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
    14.     {
    15.             var Hit : RaycastHit;
    16.             var LookingDirection = transform.TransformDirection(Vector3.forward);
    17.        
    18.             if (Physics.Raycast(transform.position.LookingDirection, Hit, Range))
    19.             {
    20.                 if (Input.GetMouseButtonDown(1))
    21.                 {      
    22.                     if (BlockSelected == 1)
    23.                     {
    24.                         var GrassBlock : Transform = Instanciate(GrassBlock, Hit.collider.transform.position + Hit.normal.normalize.Quaternion.identity);
    25.                         GrassBlock.tag = "GrassBlock";
    26.                     }
    27.        
    28.                     if (BlockSelected == 2)
    29.                     {
    30.                         var StoneBlock : Transform = Instanciate(StoneBlock, Hit.collider.transform.position + Hit.normal.normalize.Quaternion.identity);
    31.                         StoneBlock.tag = "StoneBlock";
    32.                     }
    33.                 else
    34.                 {
    35.                     Destroy(Hit.transform.gameObject);
    36.                 }
    37.             }
    38.         }
    39.     }
    40. }
    The problem is these two lines:

    Code (csharp):
    1. var GrassBlock : Transform = Instanciate(GrassBlock, Hit.collider.transform.position + Hit.normal.normalize.Quaternion.identity);
    Code (csharp):
    1. var StoneBlock : Transform = Instanciate(StoneBlock, Hit.collider.transform.position + Hit.normal.normalize.Quaternion.identity);
    The error I get is "Unknown Identifier: 'Instanciate'". I know that errors are most likely caused by typos, and I suspect I did just that. Right now, I'm now sure how to fix the error, but I will continue study the video tutorial I'm using on YouTube to figure out what I'm doing wrong. I'm trying to get the code as exactly as this guy is doing in the video, and all errors I've fixed so far were typos or do to the fact that I didn't have the rest of the source code in the second video.

    On the bright side, I am doing my 'homework' and trying at least to do some of the problem solving myself. I hate to do a project have someone else complete the code for me - I'll never learn that way. Best to learn using hands-on knowledge.
     
  6. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    Instantiate is the function name you want.
    Also, I think you've replaced a comma with a period - Instantiate takes three parameters. Since Quaternion is not a member of Vector3, I'm guessing that what you want is actually:
    Code (csharp):
    1. var GrassBlock : Transform = Instantiate(GrassBlock, Hit.collider.transform.position + Hit.normal.normalize, Quaternion.identity);
    And likewise for the other one.

    Example are useful, but you might want to spend some time first getting to know UnityScript syntax and the basic components like GameObject and Transform, so that when you're adding code you know what each piece does.

    Edit: Also, you're missing a close-bracket between lines 32 and 33, and you have an extra bracket on line 38. The code would actually compile fine with that mistake, but the results wouldn't be correct.
     
    Last edited: Feb 7, 2013
  7. bigboy4006

    bigboy4006

    Joined:
    Apr 13, 2012
    Posts:
    65
    That fixed my problem Errorsatz. Thank you. Yes, I needed to make those exact changes - with the video I'm using, sometimes it's easy to make a mistake with punctiation. As for the way 'Instatiate' was spelled, it appears as if there's a letter c in the word. Not sure why that is..
     
  8. bigboy4006

    bigboy4006

    Joined:
    Apr 13, 2012
    Posts:
    65
    Now I see why the guy how spelled the word 'Instaciate' - he spelled it wrong. I'm still trying to finish up this one video. Take time when learning this stuff - but in my eyes it's worth it!
     
  9. bigboy4006

    bigboy4006

    Joined:
    Apr 13, 2012
    Posts:
    65
    I know that basically the script allows you to place and destroy blocks with the right mouse button along with the 'E' and 'Q' keys.

    I fixed the script, and so far I can't place or destroy blocks. I keep getting an error message that says this:
    MissingFieldException: Field 'UnityEngine.Vector3.LookDirection' not found

    I know that it's throwing me an exception due to 'LookDirection', but I don't know how to fix the problem - I can't figure out how to fix it. Here's the code:

    Code (csharp):
    1. var GrassBlock : Transform;
    2. var StoneBlock : Transform;
    3. var BlockSelected : float = 1;
    4. var Range : float = 20;
    5.  
    6. function Update ()
    7. {
    8.     if (Input.GetKeyDown(KeyCode.Q))
    9.     {
    10.         BlockSelected -= 1;
    11.     }
    12.    
    13.     if (Input.GetKeyDown(KeyCode.E))
    14.     {
    15.         BlockSelected += 1;
    16.     }
    17.    
    18.     if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
    19.     {
    20.         var Hit : RaycastHit;
    21.         var LookingDirection = transform.TransformDirection(Vector3.forward);
    22.        
    23.             if (Physics.Raycast(transform.position.LookingDirection, Hit, Range))
    24.             {
    25.                 if (Input.GetMouseButtonDown(1))
    26.                 {
    27.                     if (BlockSelected == 1)
    28.                     {
    29.                         var GrassBlock : Transform = Instantiate(GrassBlock, Hit.collider.transform.position + Hit.normal.normalize, Quaternion.identity);
    30.                         GrassBlock.tag = "GrassBlock";
    31.                     }
    32.                    
    33.                     if (BlockSelected == 2)
    34.                     {
    35.                         var StoneBlock : Transform = Instantiate(StoneBlock, Hit.collider.transform.position + Hit.normal.normalize, Quaternion.identity);
    36.                         StoneBlock.tag = "StoneBlock";
    37.                     }
    38.                     }
    39.                
    40.             else
    41.             {
    42.                 Destroy(Hit.transform.gameObject);
    43.             }
    44.         }
    45.     }
    46. }
    Anyone have any idea what the problem is?

    I'm also putting up the url for the YouTube playlist I'm using right now. I'm on the second video.

    http://www.youtube.com/playlist?list=PL0DB1F113DB887485
     
    Last edited: Feb 7, 2013
  10. carljohnfred

    carljohnfred

    Joined:
    Dec 28, 2012
    Posts:
    31
    The problem is in line 23. "LookingDirection" is not a property of transform.position, it is a variable that you have declared and set in the previous line. Line 23 should read like this:

    Code (csharp):
    1.             if (Physics.Raycast(LookingDirection, Hit, Range))
    2.  
     
  11. bigboy4006

    bigboy4006

    Joined:
    Apr 13, 2012
    Posts:
    65
    I was just following what the tutorial showed. And I got an error message when I used the code you offered carl:

    Assets/Scripts/BlockMaker.js(23,44): BCE0023: No appropriate version of 'UnityEngine.Physics.Raycast' for the argument list '(UnityEngine.Vector3, UnityEngine.RaycastHit, float)' was found.


    I'm now realizing I'm needing to understand how to properly program physics into JavaScript. This looks as though it's going to take a while.
     
    Last edited: Feb 7, 2013
  12. bigboy4006

    bigboy4006

    Joined:
    Apr 13, 2012
    Posts:
    65
    Okay, I made the changes to my script like everyone told me, but I still get an error message. Here's the code:

    Code (csharp):
    1. function Update ()
    2. {
    3.     if (Input.GetKeyDown(KeyCode.Q))
    4.     {
    5.         BlockSelected -= 1;
    6.     }
    7.    
    8.     if (Input.GetKeyDown(KeyCode.E))
    9.     {
    10.         BlockSelected += 1;
    11.     }
    12.    
    13.     if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
    14.     {
    15.         var Hit : RaycastHit;
    16.         var LookingDirection = transform.TransformDirection(Vector3.forward);
    17.        
    18.             if (Physics.Raycast(LookingDirection, Hit, Range))
    19.             {
    20.                 if (Input.GetMouseButtonDown(1))
    21.                 {
    22.                     if (BlockSelected == 1)
    23.                     {
    24.                         var GrassBlock : Transform = Instantiate(GrassBlock, Hit.collider.transform.position + Hit.normal.normalize, Quaternion.identity);
    25.                         GrassBlock.tag = "GrassBlock";
    26.                     }
    27.                    
    28.                     if (BlockSelected == 2)
    29.                     {
    30.                         var StoneBlock : Transform = Instantiate(StoneBlock, Hit.collider.transform.position + Hit.normal.normalize, Quaternion.identity);
    31.                         StoneBlock.tag = "StoneBlock";
    32.                     }
    33.                     }
    34.                
    35.             else
    36.             {
    37.                 Destroy(Hit.transform.gameObject);
    38.             }
    39.         }
    40.     }
    41. }
    Even though I made corrections to the code, I still get this error:

    Assets/Scripts/BlockMaker.js(23,44): BCE0023: No appropriate version of 'UnityEngine.Physics.Raycast' for the argument list '(UnityEngine.Vector3, UnityEngine.RaycastHit, float)' was found.


    I"m I don't know what else to do to get the script to compile. Anyone have any idea what to do?
     
  13. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,245
    Is range declared anywhere as a float?

    Also is that a vector3 or not "transform.TransformDirection(Vector3.forward);" I am not sure.
     
  14. bigboy4006

    bigboy4006

    Joined:
    Apr 13, 2012
    Posts:
    65
    I did try to make LookingDirection a floating variable, which is on line 21 of the source code:

    Code (csharp):
    1. var GrassBlock : Transform;
    2. var StoneBlock : Transform;
    3. var BlockSelected : float = 1;
    4. var Range : float = 20;
    5.  
    6. function Update ()
    7. {
    8.     if (Input.GetKeyDown(KeyCode.Q))
    9.     {
    10.         BlockSelected -= 1;
    11.     }
    12.    
    13.     if (Input.GetKeyDown(KeyCode.E))
    14.     {
    15.         BlockSelected += 1;
    16.     }
    17.    
    18.     if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
    19.     {
    20.         var Hit : RaycastHit;
    21.         var float LookingDirection;
    22.         var LookingDirection = transform.TransformDirection(Vector3.forward);
    23.        
    24.             if (Physics.Raycast(LookingDirection, Hit, Range))
    25.             {
    26.                 if (Input.GetMouseButtonDown(1))
    27.                 {
    28.                     if (BlockSelected == 1)
    29.                     {
    30.                         var GrassBlock : Transform = Instantiate(GrassBlock, Hit.collider.transform.position + Hit.normal.normalize, Quaternion.identity);
    31.                         GrassBlock.tag = "GrassBlock";
    32.                     }
    33.                    
    34.                     if (BlockSelected == 2)
    35.                     {
    36.                         var StoneBlock : Transform = Instantiate(StoneBlock, Hit.collider.transform.position + Hit.normal.normalize, Quaternion.identity);
    37.                         StoneBlock.tag = "StoneBlock";
    38.                     }
    39.                     }
    40.                
    41.             else
    42.             {
    43.                 Destroy(Hit.transform.gameObject);
    44.             }
    45.         }
    46.     }
    47. }
    I still get an error message, albeit a different one:

    Assets/Scripts/BlockMaker.js(21,26): UCE0001: ';' expected. Insert a semicolon at the end.

    It now tells me I need a semicolon at the end of line 21, but I've already have one there.

    But if I change it to put the keyword 'float' in line 22, I still get an error message. Here's the new code:

    Code (csharp):
    1. var GrassBlock : Transform;
    2. var StoneBlock : Transform;
    3. var BlockSelected : float = 1;
    4. var Range : float = 20;
    5.  
    6. function Update ()
    7. {
    8.     if (Input.GetKeyDown(KeyCode.Q))
    9.     {
    10.         BlockSelected -= 1;
    11.     }
    12.    
    13.     if (Input.GetKeyDown(KeyCode.E))
    14.     {
    15.         BlockSelected += 1;
    16.     }
    17.    
    18.     if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
    19.     {
    20.         var Hit : RaycastHit;
    21.         var LookingDirection;
    22.         var float LookingDirection = transform.TransformDirection(Vector3.forward);
    23.        
    24.             if (Physics.Raycast(LookingDirection, Hit, Range))
    25.             {
    26.                 if (Input.GetMouseButtonDown(1))
    27.                 {
    28.                     if (BlockSelected == 1)
    29.                     {
    30.                         var GrassBlock : Transform = Instantiate(GrassBlock, Hit.collider.transform.position + Hit.normal.normalize, Quaternion.identity);
    31.                         GrassBlock.tag = "GrassBlock";
    32.                     }
    33.                    
    34.                     if (BlockSelected == 2)
    35.                     {
    36.                         var StoneBlock : Transform = Instantiate(StoneBlock, Hit.collider.transform.position + Hit.normal.normalize, Quaternion.identity);
    37.                         StoneBlock.tag = "StoneBlock";
    38.                     }
    39.                     }
    40.                
    41.             else
    42.             {
    43.                 Destroy(Hit.transform.gameObject);
    44.             }
    45.         }
    46.     }
    47. }
    But in this case, I get an error message for line 22, also stating I need a semicolon at the end:

    Assets/Scripts/BlockMaker.js(22,26): UCE0001: ';' expected. Insert a semicolon at the end.


    And also in this case, there's already a semicolon at the end. I don't know what to do to get the error message about the semicolon to quit popping up. I'm sure if I could figure out this little problem, I could get the script to work properly. This is frustrating!
     
  15. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,245
    you declared lookingdirection on line 21 and on line 22 you try to do it again incorrectly.

    var float LookingDirection should be var LookingDirection : float if you want a float.

    However if you use a float in the raycast when it requires a vector3 not a float.

    If this is attached to your player you could

    if (Physics.Raycast(Vector3.forward, Hit, Range))
     
  16. bigboy4006

    bigboy4006

    Joined:
    Apr 13, 2012
    Posts:
    65
    I've been trying what everyone suggests, but to no avail. From the looks of the video, the game was done in an early version of Unity. I'm not sure if that plays into the problem I'm having or not. I may quit on this playlist and try another playlist I discovered.
     
  17. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    You are declaring
    LookingDirection twice

    Code (csharp):
    1.  
    2. var LookingDirection;
    3.  
    4.         var float LookingDirection = transform.TransformDirection(Vector3.forward);
    5.  
    it should be

    Code (csharp):
    1.  
    2. var LookingDirection : float = transform.TransformDirection(Vector3.forward);
    3.  
     
  18. bigboy4006

    bigboy4006

    Joined:
    Apr 13, 2012
    Posts:
    65
    I declared the LookingDirection variable just the way you do EliteMossy. Still getting an error message about that variable though. I'm not sure what is going wrong with it. I'm not sure what to do about it.
     
  19. bigboy4006

    bigboy4006

    Joined:
    Apr 13, 2012
    Posts:
    65
    Here's the code as I have it now:

    Code (csharp):
    1. var StoneBlock : Transform;
    2. var BlockSelected : float = 1;
    3. var Range : float = 20;
    4.  
    5. function Update ()
    6. {
    7.     if (Input.GetKeyDown(KeyCode.Q))
    8.     {
    9.         BlockSelected -= 1;
    10.     }
    11.    
    12.     if (Input.GetKeyDown(KeyCode.E))
    13.     {
    14.         BlockSelected += 1;
    15.     }
    16.    
    17.     if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
    18.     {
    19.         var Hit : RaycastHit;
    20.         var LookingDirection = transform.TransformDirection(Vector3.forward);
    21.        
    22.             if (Physics.Raycast(transform.position.LookingDirection, Hit, Range))
    23.             {
    24.                 if (Input.GetMouseButtonDown(1))
    25.                 {
    26.                     if (BlockSelected == 1)
    27.                     {
    28.                         var GrassBlock : Transform = Instantiate(GrassBlock, Hit.collider.transform.position + Hit.normal.normalize, Quaternion.identity);
    29.                         GrassBlock.tag = "GrassBlock";
    30.                     }
    31.                    
    32.                     if (BlockSelected == 2)
    33.                     {
    34.                         var StoneBlock : Transform = Instantiate(StoneBlock, Hit.collider.transform.position + Hit.normal.normalize, Quaternion.identity);
    35.                         StoneBlock.tag = "StoneBlock";
    36.                     }
    37.                     }
    38.                
    39.             else
    40.             {
    41.                 Destroy(Hit.transform.gameObject);
    42.             }
    43.         }
    44.     }
    45. }
    The error is with line 23:

    Code (csharp):
    1. if (Physics.Raycast(transform.position.LookingDirection, Hit, Range))
    With the line scripted the way it is, I get this error message;

    MissingFieldException: Field 'UnityEngine.Vector3.LookingDirection' not found.

    If I rewrite line 23 to look this way:

    Code (csharp):
    1. if (Physics.Raycast(LookingDirection, Hit, Range))
    I end up getting this message:

    Assets/Scripts/BlockMaker.js(23, 44): BCE0023: No appropriate version of 'UnityEngine.Physics.Raycast' for the argument list '(UnityEngine.Vector3, UnityEngine.RaycastHit, float)' was found.

    I assume I still have more learning to do with JavaScript, because I'm not sure how to go about fixing this error. I do believe in doing my own homework, but I'm needing the help of others right now with fixing the errors. I can't even create or delete blocks in the game I'm making, and I assume it's because of this error.

    I'll list the code in it's entirety below - maybe someone will see the error:

    Code (csharp):
    1. var GrassBlock : Transform;
    2. var StoneBlock : Transform;
    3. var BlockSelected : float = 1;
    4. var Range : float = 20;
    5.  
    6. function Update ()
    7. {
    8.     if (Input.GetKeyDown(KeyCode.Q))
    9.     {
    10.         BlockSelected -= 1;
    11.     }
    12.    
    13.     if (Input.GetKeyDown(KeyCode.E))
    14.     {
    15.         BlockSelected += 1;
    16.     }
    17.    
    18.     if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
    19.     {
    20.         var Hit : RaycastHit;
    21.         var LookingDirection = transform.TransformDirection(Vector3.forward);
    22.        
    23.             if (Physics.Raycast(LookingDirection, Hit, Range))
    24.             {
    25.                 if (Input.GetMouseButtonDown(1))
    26.                 {
    27.                     if (BlockSelected == 1)
    28.                     {
    29.                         var GrassBlock : Transform = Instantiate(GrassBlock, Hit.collider.transform.position + Hit.normal.normalize, Quaternion.identity);
    30.                         GrassBlock.tag = "GrassBlock";
    31.                     }
    32.                    
    33.                     if (BlockSelected == 2)
    34.                     {
    35.                         var StoneBlock : Transform = Instantiate(StoneBlock, Hit.collider.transform.position + Hit.normal.normalize, Quaternion.identity);
    36.                         StoneBlock.tag = "StoneBlock";
    37.                     }
    38.                     }
    39.                
    40.             else
    41.             {
    42.                 Destroy(Hit.transform.gameObject);
    43.             }
    44.         }
    45.     }
    46. }
     
  20. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,245
    Try using a variable name other than range because Range is reserved (that is why it is blue when you paste the code in )

    This way if (Physics.Raycast(LookingDirection, Hit, Range)) was the correct way.
     
  21. bigboy4006

    bigboy4006

    Joined:
    Apr 13, 2012
    Posts:
    65
    I change the capital letter 'R' in Range to make it 'range' - and I also realized I had to do the same thing on line 4 when I defined 'range.' Here's the code:

    Code (csharp):
    1. var GrassBlock : Transform;
    2. var StoneBlock : Transform;
    3. var BlockSelected : float = 1;
    4. var range : float = 20;
    5.  
    6. function Update ()
    7. {
    8.     if (Input.GetKeyDown(KeyCode.Q))
    9.     {
    10.         BlockSelected -= 1;
    11.     }
    12.    
    13.     if (Input.GetKeyDown(KeyCode.E))
    14.     {
    15.         BlockSelected += 1;
    16.     }
    17.    
    18.     if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
    19.     {
    20.         var Hit : RaycastHit;
    21.         var LookingDirection = transform.TransformDirection(Vector3.forward);
    22.        
    23.             if (Physics.Raycast(LookingDirection, Hit, range))
    24.             {
    25.                 if (Input.GetMouseButtonDown(1))
    26.                 {
    27.                     if (BlockSelected == 1)
    28.                     {
    29.                         var GrassBlock : Transform = Instantiate(GrassBlock, Hit.collider.transform.position + Hit.normal.normalize, Quaternion.identity);
    30.                         GrassBlock.tag = "GrassBlock";
    31.                     }
    32.                    
    33.                     if (BlockSelected == 2)
    34.                     {
    35.                         var StoneBlock : Transform = Instantiate(StoneBlock, Hit.collider.transform.position + Hit.normal.normalize, Quaternion.identity);
    36.                         StoneBlock.tag = "StoneBlock";
    37.                     }
    38.                     }
    39.                
    40.             else
    41.             {
    42.                 Destroy(Hit.transform.gameObject);
    43.             }
    44.         }
    45.     }
    46. }
    But I still get an error message:

    Assets/Scripts/BlockMaker.js(23,44): BCE0023: No appropriate version of 'UnityEngine.Physics.Raycast' for the argument list '(UnityEngine.Vector3, UnityEngine.RaycastHit, float)' was found.


    I didn't know that 'Range' was a reserved word. I do know that I can't redefine reserved words - that's true in any programming language. But what I don't get is why Unity's game engine is having trouble with the physics. There has to be a typo on my part somewhere. I'll continue to ask help, but maybe I could also find some tutorials on YouTube that would teach me how to use JavaScript in Unity. Why I haven't thought about that yet, I don't get. Sometimes I can be a bit thick-headed when trying to understand things...
     
  22. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    When you get a message like that, a good place to start is by looking up the function in the Unity Script Reference.

    In this case, looking up Physics.Raycast, we see that there are three versions. The second one seems closest to what you have:
    Code (csharp):
    1. static function Raycast (origin : Vector3, direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : boolean
    So you can see that you need an origin vector and a direction vector. Based on what you said before, it looks like another case of switching a comma to a period - you want:
    Physics.Raycast(transform.position , LookingDirection, Hit, Range)

    Again, I would recommend getting a bit familiar with the script syntax and the basic structures like Vector3, Transform, and GameObject before trying to build a game. For example, if you know that transform.position is a Vector3, it's obvious that transform.position.LookingDirection doesn't make sense and must be a typo.
     
    Last edited: Feb 14, 2013
  23. bigboy4006

    bigboy4006

    Joined:
    Apr 13, 2012
    Posts:
    65
    I'll take a look into the Script Reference Error - I know that's the best place to start, although I haven't used it much. I'll take my time reading it and tinkering with the script to see if I can get anything to work. I may have to make some major changes before it works (but you never know...).
     
  24. bigboy4006

    bigboy4006

    Joined:
    Apr 13, 2012
    Posts:
    65
    And it does seem I still need to get familiar with some of the syntax. I've read on books about JavaScript, but all of them were for use with HTML and web pages. There's some JavaScript in Unity that I still apparently need to get used to so far. ;)
     
  25. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,245
    Javascript for unity is different, it is also called unityscript sometimes.

    I actually think the method you are using for the raycast isn't really how Minecraft works anyway.

    I would

    1. var ray : Ray = cameraObject.ScreenPointToRay(Input.mouseposition)l

    or just take the centrepoint of the screen where the crosshair is

    2. if(Physics.Raycast(ray,hit)

    3. test if the object you hit is close enough to the player for it to be able to destroy or make block
     
  26. bigboy4006

    bigboy4006

    Joined:
    Apr 13, 2012
    Posts:
    65
    You're right about JavaScript in Unity Raider. I had forgotten that it's also called UnityScript too. As for Raycast, I didn't know it wouldn't work with a Minecraft-like game. I am new to using Unity, so I still have a lot to learn (and I'm the first to admit that my knowledge of how to make games in Unity is extremely limited - I just started trying to use Unity just the past few months). Seems I'm better at modelling than I am at programming (after all, I use Blender often to model with). I've got a lot of studying to do.

    And I'll start tinkering with your suggestions Raider. I need to learn how to make a crosshairs in the Game View though - the tutorial playlist I'm using on YouTube to lean to make a Voxel-based game doesn't seem to be that good, at least not in my opinion. On the few tutorials I've used so far, I don't think that the people who make the tutorials are that good at making tutorials for Unity.
     
    Last edited: Feb 20, 2013
  27. bigboy4006

    bigboy4006

    Joined:
    Apr 13, 2012
    Posts:
    65
    I have yet to tinker with what has been suggested here. I started learning how to do this from a YouTube user calling himself MRxTeabag (he has a playlist of tutorials for making a voxel graphics-based game in Unity). I'm not so sure he's good at teaching - he's the one using Raycast in the scripts.

    Does anyone know of any good Unity voxel tutorials on YouTube - or any other website for that matter? I would buy some of the assets for voxel graphics from the Asset Store, but I don't have the money to by the assets and I don't have any credit/debit cards. Does anyone know of any good tutorials for voxel graphics in Unity? :(
     
  28. snipedcod4

    snipedcod4

    Joined:
    Jun 26, 2013
    Posts:
    1
    ]i am having a problem with the same video. i am currently a couple of stages a head and working on adding items and breaking times to the game, but there is apperently a compiler error but it is not telling me where the compiler error is!
    code below


    //Grass(1)
    var GrassBlock : Transform;
    var HeldGrassBlock : GameObject;
    var GrassBlockDestroyTime : float = 6
    //stone(2)
    var StoneBlock : Transform;
    //shovel(0)

    var WoodShovel : GametObject;
    var WoodShovelDestroyTime : float = 2;

    //misc
    var BlockSelected : float = 1;
    var range : float = 20;

    function Update ()
    {
    //changing items
    if (Input.GetKeyDown(KeyCode.Q))
    {
    BlockSelected -= 1;
    }

    if (Input.GetKeyDown(KeyCode.E))
    {
    BlockSelected += 1;
    }
    if (BlockSelected == 0)
    {
    WoodShovel.active = true;
    } else WoodShovel.active = false;
    if (Input.GetMouseButtonDown(0 Woodshovel.ShovelSelected == true) || Input.GetMouseButtonDown(1))
    {
    var Hit : RaycastHit;
    var LookingDirection = transform.TransformDirection(Vector3.forward);

    if (Physics.Raycast(transform.position, LookingDirection, Hit, 20)
    )
    if (Input.GetMouseButtonDown(1))
    {
    if (BlockSelected == 1)
    {
    var GrassBlock : Transform = Instantiate(GrassBlock, Hit.collider.transform.position + Hit.normal.normalize, Quaternion.identity);
    GrassBlock.tag = "GrassBlock";
    }

    if (BlockSelected == 2)
    {
    var StoneBlock : Transform = Instantiate(StoneBlock, Hit.collider.transform.position + Hit.normal.normalize, Quaternion.identity);
    StoneBlock.tag = "StoneBlock";
    }
    }

    else
    {
    if (BlockSelected = 0)
    (
    Destroy(Hit.transform.gameObject,WoodShovelDestroyTime);
    }
    } if (BlockSelected = 1)
    )
    (
    Destroy(Hit.transform.gameObject,);
    if (BlockSelected = 2)
    (
    Destroy(Hit.transform.gameObject,4);

    }