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

Buoyancy Script Showing Errors

Discussion in 'Scripting' started by DougieM, Jun 3, 2016.

  1. DougieM

    DougieM

    Joined:
    Jun 3, 2016
    Posts:
    2
    First post here.. very new to C# and Unity3d. I am following a tutorial I found online for creating a boat scene. One step has me creating three C# files and entering scripts they have posted. On the very first script on this page, when I enter it into a file in my Unity project it is returning 16 errors.

    I have been asking around in chat rooms and trying to figure out what I am doing wrong here but can't seem to figure it out. I apologize if the errors were not required just trying to be as thorough as possible. Any suggestions would be highly appreciated. Thanks guys.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    So all of the scripts on that page needs to be in the project at the same time - they're relying on each other.

    All of the errors are saying that you have the needed scripts, but they don't contain the correct methods. This one, for example:

     
    DougieM likes this.
  3. DougieM

    DougieM

    Joined:
    Jun 3, 2016
    Posts:
    2
    Awesome.. I will give add them all. Thanks so much for the quick response! Really appreciate the help.

    EDIT: Solved it perfectly. Thanks agian.
     
    Last edited: Jun 3, 2016
  4. Mishkawy

    Mishkawy

    Joined:
    Feb 17, 2016
    Posts:
    7
    I
    DougieM, I am glad the issue is solved, but I am stuck at the same place you are, but with a different error:
    I have 86 errors, but they are all the same:
    ]NullReferenceException: Object reference not set to an instance of an object
    BoatTutorial.ModifyBoatMesh.GenerateUnderwaterMesh () (at Assets/Scripts/ModifyBoatMesh.cs:56)
    BoatTutorial.BoatPhysics.Update () (at Assets/Scripts/BoatPhysics.cs:39)


    And that error is talking about this one line in the "ModifyBoatMesh" script:
    Code (CSharp):
    1. allDistancesToWater[j] = WaterController.current.DistanceToWater(globalPos, Time.time);
    I am very new to this, but I added the "UnderwaterMesh" to the "Underwater Obj", is this correct?
    What should I do to solve the problem?
    error.jpg
    error2.jpg

    UPDATE:
    I solved the problem after simply attaching the WaterController script to the "UnderwaterMesh" empty game object.
     
    Last edited: Sep 4, 2016
  5. edvin1989

    edvin1989

    Joined:
    Nov 28, 2019
    Posts:
    6
  6. NevadaRed

    NevadaRed

    Joined:
    Apr 4, 2020
    Posts:
    1
    Hi guys. I am suffering the same issue as mishkawy and have no idea how to solve. His fix of adding watercontroller script to empy object doesnt work for me.

    upload_2022-1-9_23-25-52.png upload_2022-1-9_23-26-13.png upload_2022-1-9_23-26-33.png
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BoatPhysics : MonoBehaviour
    6. {
    7.     //this script acts as a parent to other scripts and it placed on the boat object
    8.  
    9.     //Drags
    10.     public GameObject underWaterObj;
    11.  
    12.     //this script does everything related to boat mesh. Finds out which part is underwater etc.
    13.     private ModifyBoatMesh modifyBoatMesh;
    14.  
    15.     //mesh for debugging
    16.     private Mesh underWaterMesh;
    17.  
    18.     //The boats rigidbody
    19.     private Rigidbody boatRB;
    20.  
    21.     //Density of water the boat is travelling through
    22.     private float rhoWater = 1027f;
    23.  
    24.     void start()
    25.     {
    26.         //get the boat rigid body component
    27.         boatRB = gameObject.GetComponent<Rigidbody>();
    28.  
    29.         //initiate the script that will modify the boats mesh
    30.         modifyBoatMesh = new ModifyBoatMesh(gameObject);
    31.  
    32.         //Meshes that will be below and above the waterline
    33.         underWaterMesh = underWaterObj.GetComponent<MeshFilter>().mesh;
    34.     }
    35.  
    36.     void Update()
    37.     {
    38.         //Generate underwater mesh
    39.         modifyBoatMesh.GenerateUnderwaterMesh();
    40.  
    41.         //Display underwater mesh
    42.         modifyBoatMesh.DisplayMesh(underWaterMesh, "UnderWater Mesh", modifyBoatMesh.underWaterTriangleData);
    43.  
    44.     }
    45.  
    46.     void FixedUpdate()
    47.     {
    48.         //Add forces to the part of the boat thats below water
    49.         if (modifyBoatMesh.underWaterTriangleData.Count>0)
    50.         {
    51.             AddUnderWaterForces();
    52.         }
    53.     }
    54.  
    55.     //add forces that act on squares below the water
    56.     void AddUnderWaterForces()
    57.     {
    58.         //Get all triangles
    59.         List<TriangleData> underWaterTriangleData = modifyBoatMesh.underWaterTriangleData;
    60.  
    61.         for (int i = 0; i < underWaterTriangleData.Count; i++)
    62.         {
    63.             //This Triangle
    64.             TriangleData triangleData = underWaterTriangleData[i];
    65.  
    66.             //Calculate the buoyancy force
    67.             Vector3 buoyancyForce = BuoyancyForce(rhoWater, triangleData);
    68.  
    69.             //Add the force to the boat
    70.             boatRB.AddForceAtPosition(buoyancyForce, triangleData.center);
    71.  
    72.             //debug
    73.  
    74.             //Normal
    75.             Debug.DrawRay(triangleData.center, triangleData.normal * 3f, Color.white);
    76.  
    77.             //Buoyancy
    78.              Debug.DrawRay(triangleData.center, buoyancyForce.normalized * -3f, Color.blue);
    79.  
    80.         }
    81.     }
    82.  
    83.     //the buoyancy force so the boat can float
    84.     private Vector3 BuoyancyForce(float rho, TriangleData triangleData)
    85.     {
    86.         //The water force is present even when not flowing
    87.      
    88.         //rho - density of the medium you are in
    89.         //g - gravity
    90.         //V - volume of fluid above the curver surface
    91.  
    92.         //V = z * S * n
    93.         // z - distance to surface
    94.         // S - surface area
    95.         // n - normal to the surface
    96.         Vector3 buoyancyForce = rho * Physics.gravity.y * triangleData.distanceToSurface * triangleData.area * triangleData.normal;
    97.  
    98.         //below cancels out horizontal buoyancy forces
    99.         buoyancyForce.x = 0f;
    100.         buoyancyForce.z = 0f;
    101.  
    102.         return buoyancyForce;
    103.     }
    104.  
    105. }

    I am getting an error at cs39 which is modifyBoatMesh.GenerateUnderwaterMesh();

    it needs a reference but not sure where it needs to be. thank you in advance.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,375
    Please don't necro-post to old threads to fix your typographic mistakes.

    Remember: correct capitalization matters:

    If you expect Unity call the above function, you have to spell and capitalize it properly. That's not optional.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Since it appears you are hammering this code in from some other source, here is how to ensure you are not just wasting your time:

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!