#Can't GetComponent?

1 messages · Page 1 of 1 (latest)

novel spindle
#

I'm trying to get my Recoil Script but I can't GetComponent.
I've tried transform.GetComponentInParent<Recoil>();
I've tried FindObjectOfType<Recoil>();
I've tried transform.parent.parent.parent.GetComponent<Recoil>();
I've tried transform.Find("Player/Camera Holder").GetComponent<Recoil>();

The error comes out the same.
NullReferenceException: Object reference not set to an instance of an object.

Any help is appreciated!

#

I'm trying to get the reference from my MuzzleFlashes

#

So the sniper shot /w trail, rocketlauncher trail, etc.

#

Is the reason cause those gameObjects are inactive?

#

I only set them active when i fire my weapon

ebon coral
#

On which game object is your recoil component script attached? What about the component that’s referencing the recoil component? For GetComponent() to work, the two scripts must be put on the same GameObject.

#

In any case…

ebon coral
#

Hold on. let me find the bot command that says you shouldn’t even be using Find() or GetComponent() anyway.

#

[]nofind

sinful pecanBOT
#

Don't use Find methods!

Find methods in Unity are an often-tempting solution to referencing a scene object. However, there is always a better solution.
Whenever you call a Find method, Unity must traverse your entire scene hierarchy and check every single object until it finds a match; and the methods which return arrays will always traverse the entire hierarchy regardless.
This is inefficient! It means that the more objects you add to your scene, the slower these methods become; and it gets even worse if you are calling them multiple times.

A non-exhaustive list of Find methods to avoid:
https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html
https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html
https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html

To read about a better solution, type []getareference or []getref

ruby stone
#

[]getref

sinful pecanBOT
#

How to get a reference

"How do I access a variable from another script?" is usually one of the first questions we ask when learning Unity. Whether you are C# beginner or a C# professional, understanding the way Unity does things differently can be confusing.

🌳 If the objects are already present in the scene hierarchy...
... then you can use the SerializeField attribute on a field, and assign it by dragging the object which has the script you want to access onto the field slot. This also works if the two scripts are on the same object.

[SerializeField] private SomeScript someScript;

prefab If the objects are instantiated prefabs...
... then you can use a form of injection after you call Instantiate:
SomeScript.cs

public GameManager TheManager { get; set; }

GameManager.cs

var clone = Instantiate(prefab);
clone.GetComponent<SomeScript>().TheManager = this;
green igloo
# novel spindle I'm trying to get my Recoil Script but I can't GetComponent. I've tried transfo...

okay, transform.Find isn't that bad, but the problem is that you are heavily reliant on correct naming
and transform.Fnd checks the transform child hierarchy (compared to other find methods which check all scene elements)

I'd recommend to directly assign the reference in your script through the inspector,

and instead of guessing methods, just check the official docs
https://docs.unity3d.com/ScriptReference/Transform.Find.html
GetComponent only looks for components on the same object
GetComponentInParent should work if your script is on DrumGun and looks for a script on Camera Holder

#

also here is a related thread in which I write more about find methods
#1058427287390920785 message

green igloo
# novel spindle Is the reason cause those gameObjects are inactive?

and yeah if you look at the documentation, certain find methods like GameObject.Find only return active objects
with FindObjectsOfType you have a method overload that takes a bool where you can specify if you want to include inactive objects. if you pass no bool, I think the default is to not find inactive objects

novel spindle
#

The recoil script I'm trying to get is in CameraHolder

#

I've tried GetComponentInParent but that didnt work, I still got an error

#

I was trying to avoid using the drag method but I might have to do that

green igloo
#

that should work tbh, GetComponentInParent

#

(always check the docs and compare if the described behaviour matches your actual hierarchy/setup)

#

so just call it and pass true to the method or sth

novel spindle
#

GetComponentInParent doesn't work for some reason. I think its maybe cause my muzzle flash goes active and then inactive right after?

#

The drag method works tho

green igloo
#

i described why it might not work, but if it still doesn't work with passing true, dunno, there must be something else that's off in your setup them

#

but unless you assembly the muzzle connection during runtime, and not in the editor, direct reference is usually preferred as it's faster too