- Home /
Help accessing script parameter with trigger
Hi, pretty basic scripting question- I have a trigger with a script attached to it:
function OnTriggerEnter (player : Collider) {
if (player.tag=="Player")
player.PlatformerPhysics.gravityMultiplier = 0;
}
I'm trying to access a script called PlatformerPhysics and alter a parameter called gravityMultiplier. It just isn't working for some reason. A console error reads:
"Null referenceException: Object reference not set to an instance of an object."
Any idea what I'm doing wrong here? Thanks.
Answer by cdrandin · Aug 25, 2013 at 05:56 PM
function Start ()
{
platformPhysicsScript = GameObject.Find("Player").GetComponent(PlatformerPhysics);
}
function OnTriggerEnter (player : Collider) {
if (player.tag=="Player")
platformPhysicsScript .gravityMultiplier = 0;
}
This should do it.
Let me explain. You want to get reference of your script so we need a way of doing that. We use GetComponent(...)
for just that. so platformPhysicsScript
is our reference to the script. Now lets say the location the script was attached to was not the player. Then we need to find the object the script is attached too. Then we use GameObject.Find(...)
Checkout http://docs.unity3d.com/Documentation/ScriptReference/GameObject.html Main things you want to focus on is the GetComponent
method and the Find
methods, either it be Find or FindWithTag.
I see that this script should definitely work, however I still get an error:
$$anonymous$$ identifier: "PlatformerPhysics"
Doesn't make sense to me, as that is the scripts name.
I've found a reference to the PlatformerPhysics script using another .cs file. Does this provide any insight as to why accessing the PlatformerPhysics script using the code above hasn't worked?
PlatformerPhysics physics = other.gameObject.GetComponent<PlatformerPhysics>();
Well I was able to achieve my goal using this command in c#. I am still curious as to why the Java script was failing...
Could have failed due to syntax errors. I am not sure of JS ways, so that could be it but glad you were able to get it working and sorry I was not able to 100% solve your problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220613112938im_/https://answers.unity.com/themes/thub/images/avi.jpg)