- Home /
Duplicate Question
I need help referencing another script so I can disable it (C#)
Hi, I need help on - you guessed it - disabling a script using another script in C#. I want the playermovement thing to appear in Unity so I can reference it with my PlayerMovement script, but it doesn't appear. Any help? PLEASE? Tip: I'm a noob and got this off a tutorial. For some reason it worked for who made the tutorial, but it didn't work for me. Here's my code:
using UnityEngine;
public class PlayerCollision : MonoBehaviour
{
public PlayerMovement movementtrash;
void OnCollsionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Obstacle")
{
movementtrash.enabled = false;
}
}
}
I need a way to make the PlayerMovement appear in unity so I can reference it with my script and disable it. If you have questions, ask right ahead, I seriously need help.
Answer by SohailBukhari · Jun 09, 2017 at 07:02 AM
Attach PlayerMovement script on the Player and publicly assign in the inspector. you can find reference by calling but make sure PlayerMovement exist in the current scene where you are checking collision.
FindObjectOfType<PlayerMovement>().enabled = false;
other thing is that you can make static instance and then enable/disable.
using UnityEngine;
public class PlayerCollision : MonoBehaviour
{
public PlayerMovement movementtrash;
void OnCollsionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Obstacle")
{
movementtrash.enabled = false;
}
}
}
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
"Only assignment, call, increment, decrement and new object expressions can be used as statements" 1 Answer
Distribute terrain in zones 3 Answers
Persistent mouselook disabling error? 0 Answers
An object reference is required for non-static field, method, or property 1 Answer