• Register
search
Log In
1 vote
21 views

Problem:

I am beginner to C# with Unity engine. I have already started learning C# with Unity engine.

I have tried to write the very basic script as shown below for your reference:

using UnityEngine;
using System.Collections;
public class scriptMyBall : MonoBehaviour {
    // Use this part for initialization
    void Start () {
        Rigidbody.AddForce(0,1000f,0);
    }
   // Update is called only once per frame
    void Update () {
   }
}

But it is showing me following error:

Assets/Scripts/scriptMyBall.cs(8,27): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3, UnityEngine.ForceMode)'

I have tried to do research on above error and tried to fix it on my own but unable to fix the Unity Engine related error. I need Unity Engine’s expert who can guide me in fixing above error.

6.6k points
reopened by

2 Answers

1 vote
 
Best answer

Solution:

I am good in Unity engine. To fix your error you will simply need to instantiate your class Rigidbody before you try to access the non-static field such as the AddForce.

You can refer the documentation from the link given below for your help:

using UnityEngine;
using System.Collections;
public class MyExampleClass : MonoBehaviour {
    public float mythrust;
    public Rigidbody myrb;
    void Start() {
        // Gets the instance here and then stores it as the class member.
        myrb = GetComponent<Rigidbody>();
    }
    void MyFixedUpdate() {
        // Re-use your member to access your non-static method
        myrb.AddForce(transform.forward * mythrust);
    }
}

More Details : http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

34.5k points
selected by
0 votes

Solution:

You need to instantiate your class Rigidbody before accessing a non-static field such as AddForce.

From the documentation bellow :

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public float thrust;
    public Rigidbody rb;
    void Start() {
        // Get the instance here and stores it as a class member.
        rb = GetComponent<Rigidbody>();
    }
    void FixedUpdate() {
        // Re-use the member to access the non-static method
        rb.AddForce(transform.forward * thrust);
    }
}

 

or  You can add a local property to a rigid body and set it within the editor or use

var rigidBody = GetComponenet<RigidBody>();
rigidBody.Addforce(...)

to get the local instance of the component via code rather than the editor.

 

Another way of doing this you should make your audioSounds and minTime members static:

public static List<AudioSource> audioSounds = new List<AudioSource>();
public static double minTime = 0.5;

However  you may consider using singleton objects instead of static members instead:

public class SoundManager : MonoBehaviour
{

    public List<AudioSource> audioSounds = new List<AudioSource>();
    public double minTime = 0.5;

    public static SoundManager Instance { get; private set; }

    void Awake()
    {
        Instance = this;
    }

    public void playSound(AudioClip sourceSound, Vector3 objectPosition, int volume, float audioPitch, int dopplerLevel)
    {    
        bool playsound = false;
        foreach (AudioSource sound in audioSounds) // Loop through List with foreach
        {  
            if (sourceSound.name != sound.name && sound.time <= minTime)
            {
                playsound = true;
            }
        }

        if(playsound) {
            AudioSource.PlayClipAtPoint(sourceSound, objectPosition);
        }

    }
}

 

12.8k points

Related questions

0 votes
3 answers 163 views
163 views
Problem : Why is the below error occurring? An object reference is required for the nonstatic field, method, or property 'WindowsApplication1.Form1.setTextboxText(int)
asked Jan 18 jwilliam 3.9k points
1 vote
1 answer 65 views
65 views
Problem : Looking for the solution on bellow problem setting the parent of a transform which resides in a prefab is disabled to prevent data corruption
asked Nov 8, 2019 peterlaw 6.9k points
0 votes
2 answers 232 views
232 views
Problem : I am new to Unity5. I am facing following error while trying to do setDestination. "SetDestination" can only be called on an active agent that has been placed on a NavMesh. UnityEngine.NavMeshAgent:SetDestination(Vector3) CompleteProject.EnemyMovement:Update() (at Assets/ ... ; } } } } May I know what is wrong with the above code? Does anyone have any solution on my issue?
asked Nov 27, 2019 alecxe 7.5k points
0 votes
1 answer 571 views
571 views
Problem : I am a beginner in programming and I am trying to write some code but when my code executes, it immediately throws the NullReferenceException as shown below: &ldquo;7 days to die nullreferenceexception object reference not set to an instance of an object&rdquo; I am unable to fix this error. Can somebody guide me in fixing above error?
asked Feb 17 mphil 2.3k points
0 votes
1 answer 158 views
158 views
Problem : I am trying to create the test app using unity but facing following issue: readpixels was called to read pixels from system frame buffer, while not inside drawing frame.
asked Nov 14, 2019 peterlaw 6.9k points