How to reassign a new script reference to the same variable that previously had the same script with another reference?
So I supposedly have simple problem that I have not been able to fix at all.
if (gnomeMonster == null) {
gnomeMonster = GameObject.Find(name);
gnomeMonsterScript = gnomeMonster.GetComponent<MonsterScript>();
print(gnomeMonsterScript);
gnomeMonsterScript.setOwner("Gnome");
}
The error I get is that gnomeMonsterScript is Null when I print it and I do not understand why at all. This code sample is part of a greater script that manages most of my card game that I created. Basically at the beginning on turn 1, I need to show 2 monsters cards on the webcam, the first monster will use the code sample posted so the script gets assigned without no problems and the game works perfectly.
The problem comes on turn 2 where I need to show 2 more monster cards. When the code of the first monster card is executed (sample code), now when assigning the script I get the Null whereas on the turn 1 I got the proper script name. So after printing the message, I get an Unity error and the game completely stops working.
Obviously, all monsters do have a MonsterScript component so this is not the problem. Also, this happens with absolutely any monster on the second turn.
The error is the following:
Exception in callback: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object at GameSceneScript.FoundMonster (System.String name) [0x00036] in ...Assets\Resources\Scripts\GameSceneScript.cs:207 at CustomMarkerDetectionScript.OnTrackingFound () [0x0000d] in ...Assets\Resources\Scripts\CustomMarkerDetectionScript.cs:55 at CustomMarkerDetectionScript.HandleTrackableStatusChanged () [0x0001c] in ...Assets\Resources\Scripts\CustomMarkerDetectionScript.cs:37 at CustomMarkerDetectionScript.OnTrackableStatusChanged (Vuforia.TrackableBehaviour+StatusChangeResult statusChangeResult) [0x00018] in ...Assets\Resources\Scripts\CustomMarkerDetectionScript.cs:33 at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in :0
--- End of inner exception stack trace --- at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00048] in :0 at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in :0 at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x000e7] in :0 at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00008] in :0 at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000] in :0 at Vuforia.DelegateHelper.InvokeDelegate (System.Delegate action, System.Object[] args) [0x0000f] in :0 UnityEngine.Debug:LogError(Object) Vuforia.DelegateHelper:InvokeDelegate(Delegate, Object[]) Vuforia.DelegateHelper:InvokeWithExceptionHandling(Action`1, StatusChangeResult) Vuforia.<>c_DisplayClass30_0:b_0(Action`1) Vuforia.IEnumerableExtensionMethods:ForEach(IEnumerable`1, Action`1) Vuforia.TrackableBehaviour:UpdateTrackableStatus(Status) Vuforia.TrackableBehaviour:OnTrackerUpdate(Status, StatusInfo) Vuforia.StateManager:UpdateTrackableStates(TrackableResultData[]) Vuforia.StateManager:UpdateTrackablePoses(Vector3, Quaternion, TrackableResultData[], VuMarkTargetResultData[], TrackableIdPair, Int32, Boolean) Vuforia.StateManager:UpdateTrackablePoses(Vector3, Quaternion, TrackableResultData[], VuMarkTargetResultData[], TrackableIdPair, Int32) Vuforia.VuforiaManager:UpdateTrackers(ManagedFrameState) Vuforia.VuforiaManager:Update(VuforiaScreenOrientation, Boolean&) Vuforia.VuforiaARController:UpdateStatePrivate(Boolean, Boolean) Vuforia.VuforiaARController:UpdateState(Boolean, Boolean) Vuforia.DigitalEyewearARController:Update() System.Delegate:DynamicInvoke(Object[]) Vuforia.DelegateHelper:InvokeDelegate(Delegate, Object[]) Vuforia.DelegateHelper:InvokeWithExceptionHandling(Action) Vuforia.VuforiaBehaviour:Update()