- Home /
Accessing a Script from all objects with that script
I am trying to make my own realistic gravity, and need to access all relavent game objects, but I am having lots of problems
My script is:
public class Gravity : MonoBehaviour
{
public GameObject[] NewGravObj;
public float Grav
// Start is called before the first frame update
void Start()
{
NewGravObj = (Gravity)gameObject.GetComponent(typeof(Gravity));
}
// Update is called once per frame
void Update()
{
for (int i = 0; i< NewGravObj.Length; i++){
if (NewGravObj[i].GetComponent("Gravity")!=null)
{
//Gives easy access to the other Gravity Script
Gravity GravForce = NewGravObj[i].GetComponent("Gravity");
f (GravForce.Grav * Mathf.Pow(1.0f - 0.00002f, Vector3.Distance(transform.position, NewGravObj[i].transform.position)) <= 0.2f) {
}
}
}
}
}
I am running into problems with my only line in Start(), and when I make the GravForce variable. Does anyone know how to create a list of all GameObjects and call a script from a game object?
Answer by ShadyProductions · Feb 19, 2020 at 09:11 AM
I don't really know what you're trying to attempt in your start method, You are trying to retrieve a Gravity script from the gameobject the gravity script is attached to.
So a reference to itself? Which is pointless, and then you are trying to put that reference into a GameObject[] type variable, which obviously it cannot because it is of type Gravity.
You will have to be a bit more specific with which objects you exactly want. You could do something like: https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html
NewGravObj = FindObjectsOfType<GameObject>(); //Will return and store all gameobjects in the scene in the NewGravObj array.
Note that FindObjectsOfType is not a very performant method call, and it should not be overdone.
@ShadyProductions In the start method I am trying to get a list of all GameObjects in the scene, $$anonymous$$us the one checking at the time, then in the update method, I am trying to access the variables in the other Gravity scripts. I hope that makes more sense.
Not to judge, but what you're trying to do seems a bit out of scope of your capabilities when it comes to C#. Why don't you just use unity's physics engine?
What you want is this:
using System.Linq;
using UnityEngine;
public class Gravity : $$anonymous$$onoBehaviour
{
public float Grav;
private int _uniqueInstanceId;
void Start()
{
_uniqueInstanceId = GetInstanceID();
if (GravityObjectDatabase.GravityObjects == null)
{
// Get all gravity objects
GravityObjectDatabase.GravityObjects = FindObjectsOfType<Gravity>()
.Select(a => new GravityObject(a.gameObject, a))
.ToArray();
}
}
// Update is called once per frame
void Update()
{
// If one of the gameobjects is destroyed you must also remove them from the GravityObjects array (preferably before the object is destroyed (consider using List ins$$anonymous$$d of array))
foreach (var gravObject in GravityObjectDatabase.GravityObjects)
{
// Skip the object if it is the same as the current class.
if (gravObject.GameObject.GetInstanceID() == _uniqueInstanceId) continue;
Gravity GravForce = gravObject.GravityScript;
if (GravForce.Grav * $$anonymous$$athf.Pow(1.0f - 0.00002f, Vector3.Distance(transform.position, gravObject.GameObject.transform.position)) <= 0.2f)
{
// Do your stuff
}
}
}
}
public class GravityObject
{
public GameObject GameObject;
public Gravity GravityScript;
public GravityObject(GameObject obj, Gravity script)
{
GameObject = obj;
GravityScript = script;
}
}
public static class GravityObjectDatabase
{
public static GravityObject[] GravityObjects;
}
Thank you for this code, it is definitely beyond my skill level, but I think I understand it.
To answer your question: I am trying to get '3d' gravity where object fall towards others, even if it's y coordinate has a lower value than the object it is falling towards
Answer by logicandchaos · Feb 19, 2020 at 12:42 PM
You declared NewGravObj as a list of GameObjects, you are trying to assign it as a Gravity component. What you need is FindObjectsOfType, this will return an array of all the objects of that type you want. This question has been answered on here a few times already, it came up when I typed your question into google. https://answers.unity.com/questions/527447/find-gameobjects-with-a-certain-script.html https://answers.unity.com/questions/46283/way-to-get-all-object-with-a-certain-componentscri.html Though I feel like you should study lists a little to get a better handle on them.
I have tried some of the methods I have found on google, so far none of them have worked, But I will look at this link