Some basic Scripting Troubles in C# null reference
I have a bool called "WeaponTrail" on my animator which is on the current game object. I am using the XWeaponTrail asset, and am trying to turn the weapon trail off and on for each melee swing (each attack animation state). The sword has the weapon trail attached to it. The sword is attached to the model, which is all on the same game object like normal. I am recieving a null reference exception with the following code. I'm pretty new in coding, any help would be appreciated.
using UnityEngine;
using System.Collections;
using Xft;
public class SwordTrailSwitch : MonoBehaviour {
XWeaponTrail xwep;
Animator anime;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Swordbool ();
}
//CGP turn on and off weapon trail CGP
void Swordbool()
{
if (anime.GetBool ("WeaponTrail") == true) {
xwep.Activate ();
}
if(anime.GetBool("WeaponTrail") == false) {
xwep.Deactivate ();
}
}
}
Answer by Monkiman300 · Aug 25, 2016 at 02:00 AM
Are you trying to activate the sword trail as a game object? If that is the case you need xwep.SetActive(true)
and xwep.SetActive(false)
respectively. Hope this helped, if not let me know and put the error message in here too, it helps understand the issue for us. :)
Thank you for taking the time to reply. I'll try to include all the information i can.
I am currently using a system that equips weapons (game objects) on a handler. To use the weapon trail system, you are instructed to attach the prefab to the weapon, and add a start and end point. It also has API instructions which i'll include below. $$anonymous$$y thought was to make a bool that turned true at the beginning of a state and false at the end of a state, and to attach that to each animation state in the $$anonymous$$ecanime State $$anonymous$$achine. Then add a script to the weapon that would use the Activate() if the Bool (WeaponTrail) was true and Deactivate() if the Bool (WeaponTrail) was false. I did this because i won't know what weapon i'll have equiped, and only want to activate the weapon the character is holding. The Bool flipping script is working fine, the SwordTrailSwitch script (the one i posted about) is not though. The SwordTrailSwitch script is attached and is a child of the weapon (which is a game object)
This is the API instructions
API:
Activate(): Activate trail
Deactivate(): Stop trail immediately
StopSmoothly(float fadeTime): Stop trail smoothly.
*NOTE YOU NEED TO I$$anonymous$$PORT THE NA$$anonymous$$E SPACE, for example, using Xft; in C#*
Here is the Error $$anonymous$$essage i am getting....
NullReferenceException: Object reference not set to an instance of an object
SwordTrailSwitch.Swordbool () (at Assets/XWeaponTrail/Scripts/SwordTrailSwitch.cs:21)
SwordTrailSwitch.Update () (at Assets/XWeaponTrail/Scripts/SwordTrailSwitch.cs:15)
Any help would be appreciated!