Home > Blockchain >  Unable to reference variable from other GameObject for some reason UNITY C#
Unable to reference variable from other GameObject for some reason UNITY C#

Time:11-01

When trying to reference another object, I get this error:

Assets/Scripts/Gravity.cs(55,63): error CS1061: 'GameObject' does not contain a definition for 'mass' and no accessible extension method 'mass' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)

I'm very new to unity and C# as a whole so I'm not sure what is going on here. Any help would be greatly appreciated.

Here's the code

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

public class Gravity : MonoBehaviour
{
    [SerializeField] private GameObject otherBody;
    public Vector3 initialVelocity;
    Vector3 currentVelocity;
    bool simulate = false;
    bool initial = false;
    public int mass;
    int constant;

    void Awake() 
    {
    }

    // Start is called before the first frame update
    void Start()
    {
        constant = Global.GravitationalConstant;
    }

    // Update is called once per frame
    void Update()
    {
        constant = Global.GravitationalConstant;

        if (Input.GetKeyDown(KeyCode.Space))
        {
            simulate = !simulate;

            if (initial == false) 
            {
                currentVelocity = initialVelocity;
            }

            initial = true;
        }

        if (simulate)
        {
            GravityUpdate();
            // currentVelocity = initialVelocity;
        } 
    }
 
    void GravityUpdate()
    {
        float sqrDist = (otherBody.transform.position - transform.position).sqrMagnitude;
        Vector3 moveDir = (otherBody.transform.position - transform.position).normalized;
        
        Vector3 force = moveDir * constant * mass * otherBody.mass / sqrDist;
        currentVelocity  = force * Time.deltaTime;
        transform.position  = currentVelocity * Time.deltaTime;
    }
}

I've tried making reference to the script name, as in - otherBody.Gravity.mass but it just then says that Gravity is undefined.

CodePudding user response:

Change GameObject to Gravity, or get the Gravity from the GameObject.

[SerializeField] private Gravity _other;

// or

[SerializeField] private GameObject _other;
private Gravity _otherGravity;

private void Start()
{
    _otherGravity = _other.GetComponent<Gravity>();
}

CodePudding user response:

Gravity.otherBody is of type GameObject and you are trying to access its member 'mass'. But GameObject has no member 'mass'.

This is what the error tells you.

What you want to do is to access the member 'mass' of the component Gravity which is attached to that GameObject you assigned at Gravity.otherBody.

To solve this you can do two things.

You change [SerializeField] private GameObject otherBody; to [SerializeField] private Gravity otherBody;

Now you can assign that other GameObject containing the component Gravity in the inspector. Naming the member otherGravity would keep your code easier to maintain.

If you change the name, in this line it should be change accordingly.

Vector3 force = moveDir * constant * mass * otherBody.mass / sqrDist;

If you need access to the GameObject of Gravity.otherBody, or its Transform, this can be done by:

GameObject go = otherBody.gameObject;
Transform  t  = otherBody.transform;

Another, less desirable, option is to get the component Gravity from the private GameObject otherBody.

Gravity gravity = otherBody.GetComponent<Gravity>();
int     m       = gravity.mass;
  • Related