Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 07, 2012 at 10:35 AM by ragnaros100 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by ragnaros100 · Mar 09, 2012 at 06:03 PM · destroyplaystopkinematicontrigger

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();    
                 }
         }
     }
 }
  
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlwaysSunny · Mar 09, 2012 at 06:17 PM 1
Share

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.

avatar image ragnaros100 · Mar 09, 2012 at 07:27 PM 0
Share

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...

1 Reply

  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ragnaros100 · Mar 10, 2012 at 09:19 AM 0
Share

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges