Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;

public class Floating : MonoBehaviour
{
    public Rigidbody rb;
    public float depthBefSub;
    public float displacementAnt;
    public int floaters;
    public float waterOrag;
    public float waterAngularOrag;
    public WaterSurface water;
    WaterSearchParameters Search;
    WaterSearchResult SearchResult;

    private void FixedUpdate()
    {
        water += OceanE.Ocean;

        rb.AddForceAtPosition(Physics.gravity / floaters, transform.position, ForceMode.Acceleration);

        Search.startPosition = transform.position;

        water.FindWaterSurfaceHeight(Search, out SearchResult);

        if (transform.position.y < SearchResult.height)
        {
            float displacementMulti = Mathf.Clamp01(SearchResult.height - transform.position.y / depthBefSub) * displacementAnt;
            rb.AddForceAtPosition(new Vector3(0f, Mathf.Abs(Physics.gravity.y) * displacementMulti, 0f), transform.position, ForceMode.Acceleration);
            rb.AddForce(displacementMulti * -rb.velocity * waterOrag * Time.fixedDeltaTime, ForceMode.VelocityChange);
            rb.AddForce(displacementMulti * -rb.angularVelocity * waterAngularOrag * Time.fixedDeltaTime, ForceMode.VelocityChange);
        }
    }
}


What I have tried:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OceanE : MonoBehaviour
{
    public GameObject Ocean;
}
Posted
Updated 2-Jul-23 19:21pm

I don't do Unity, however, that error in C# usually means that you have not initialised the class before using it. Somewhere in your code you need to create a reference:
C#
private OceanE oceanE;

and then initialise it:
C#
OceanE = new oceanE();

Same with inner objects:
C#
public class OceanE : MonoBehaviour
{
    public GameObject Ocean = new GameObject();
}

Once initialised, then you can work with it...

Not sure what this line is doing though:
C#
water += OceanE.Ocean;

Maybe, the GameObject overrides the operator + for objects... you can read more about it here: Operator overloading - Define unary, arithmetic, equality, and comparison operators. | Microsoft Learn[^]
 
Share this answer
 
As error shares, you are calling a non-static item as a static. You can solve this by:
Option 1:
Have OceanE as static class such that you can directly access it's properties. This depends on the design choice.

Option 2:
Initialize OceanE before using it's properties. (As shared by GraemeG above). You mentioned in what you tried about declaring the variable but you also need to initialize it.
C#
public GameObject Ocean; //Access modifier as per need

// later before using the object, initialize it, like:
OceanE o = new OceanE();
water += o.Ocean;

Details of this error with multiple examples: Compiler Error CS0120 | Microsoft Learn[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900