- Home /
Problem is not reproducible or outdated
how to access a script, and affect only one of the gameobjects its attached to?
I have this huge problem. Even my teacher cant figure this one out. I basicly have my character spawning gadgets, which has a sperical collider that defines its range. The gadget puts nearby gameobjects with the tag "obstacle" in a 'stasis' mode. (stops the animation and sets the rigidbody to kinematic) Now, that works perfectly. The problem comes when I remove my gadget again. The gadget disapears and all is great but if it has stopped an "obstacle". The "obstacle doesnt restart the animation and the rigidbody stays kinematic. All I get in the Log is a NullRefferenceException.
The code consists of 3 scripts:
This first script is attached to all of my "obstacles":
using UnityEngine;
using System.Collections;
public class Private_position_Update : MonoBehaviour {
public Vector3 pos;
public Quaternion rot;
Quaternion rigRot;
void Start ()
{
pos = transform.position;
rot = transform.rotation;
rigRot = rigidbody.rotation;
}
void UseTime()
{
transform.position = pos;
transform.rotation = rot;
rigidbody.velocity = new Vector3(0,0,0);
rigidbody.rotation = rigRot;
rigidbody.angularVelocity = new Vector3(0,0,0);
animation.Stop();
animation.Play();
rigidbody.isKinematic = false;
}
public void RemoveOfGadget()
{
Debug.Log("Restart the animation and kinematicness");
animation.Play();
rigidbody.isKinematic = false;
}
}
This next script is attached to my gadget:
using UnityEngine;
using System.Collections;
public class Pickupgadget_Reciver : MonoBehaviour {
public GameObject ThisGameobject;
public Private_position_Update PrivatePositionUpdate;
void Awake(){
}
void PickMeUp()
{
Destroy(ThisGameobject);
PrivatePositionUpdate.RemoveOfGadget();
}
}
And last one is also attached to my gadget and is the script that puts the "obstacles" in 'stasis'
using UnityEngine;
using System.Collections;
public class StasisColliderStasis : MonoBehaviour {
private Pickupgadget_Reciver PickupScript;
public GameObject Target;
public bool status;
void Awake(){
status = false;
}
void Start()
{
PickupScript = gameObject.GetComponent<Pickupgadget_Reciver>() as Pickupgadget_Reciver;
}
//The collider defines the range of the device.
void OnTriggerStay (Collider other)
{
//if the "intruder" has the tag "obstacle". It'll freeze position and rotation
if (other.transform.tag == "obstacle")
{
Debug.Log("I see you! >:D - " + other.transform.name);
if (other.rigidbody != null)
{
other.rigidbody.isKinematic = true;
}
if (other.animation != null)
{
other.animation.Stop();
}
}
}}
EDIT: I've changed the StasisColliderStasis script to OnTriggerEnter and OnTriggerExit
using UnityEngine;
using System.Collections;
public class StasisColliderStasis : MonoBehaviour {
private Pickupgadget_Reciver PickupScript;
public GameObject Target;
public bool status;
void Awake(){
status = false;
}
void Start()
{
PickupScript = gameObject.GetComponent<Pickupgadget_Reciver>() as Pickupgadget_Reciver;
}
//The collider defines the range of the device.
void OnTriggerEnter (Collider other)
{
//if the "intruder" has the tag "obstacle". It'll freeze position and rotation
if (other.transform.tag == "obstacle")
{
Debug.Log("I see you! >:D - " + other.transform.name);
if (other.rigidbody != null)
{
other.rigidbody.isKinematic = true;
}
if (other.animation != null)
{
other.animation.Stop();
}
}
}
void OnTriggerExit (Collider other)
{
if (other.transform.tag == "obstacle")
{
if (other.rigidbody != null)
{
other.rigidbody.isKinematic = false;
}
if (other.animation != null)
{
other.animation.Play();
}
}
}
}
A couple of points come to $$anonymous$$d real quick: With this code, you shouldn't be keeping the objects in stasis using OnTriggerStay - use OnTriggerEnter and OnTriggerExit ins$$anonymous$$d to begin and end the stasis effect unless there's some reason not to. In response to your question, is your stasis-causing object calling an endStasis function on colliding objects OnTriggerExit AND just before it is destroyed? Using OnCollisionEnter/Exit will simplify things a bit, plus you can probably call the same stasis-affected object's function both OnTriggerExit and when you destroy the stasis-causer.
I've changed it now, but I still get a NullReffenceException error. Its like the gadget gets destroyed before it can send the message out (even with OnTriggerExit) :/ great suggestion though. I honestly feel stupid for not thinking of that...
Answer by mpavlinsky · Mar 09, 2012 at 08:16 PM
How are you assigning the PrivatePositionUpdate variable in the Pickupgadget_Reciver script? Is it assigned in the editor or did you intend to assign it in the StasisColliderStasis script? If that is unassigned it would definitely result in the behavior you're describing.
I've also thought of that. But I dont know how to assign it proberly... $$anonymous$$y problem is that I have several gameobjects with the Private_position_Update script on it.
Follow this Question
Related Questions
In game pause menu 2 Answers
Another animation is not working 0 Answers
how to turn on and off particles with key press 0 Answers
How do I restart enemy spawn when boss is Destroyed in Unity C# version 5.2 1 Answer
Automatically playing animations 1 Answer