How to activate a disabled script onCollisionEnter

Hello everybody

I am new to Unity and C#, and I have spent the last few weeks learning the unity interface, and slowly learning some C# concepts. I was having a fair amount of success building a “proto-type” game, but I recently ran into a problem that I can not figure out how to fix.

What I am attempting to accomplish, is to have a player controlled cube collide with a stationary sphere, and when the two objects collide, to destroy the cube, and to activate a disabled player controller script on the sphere.

In my current test project I have a cube and sphere, each has a rigidbody componenet and a character controller script attached to them.

This is the control script that is attached to each of the objects:

using UnityEngine;
using System.Collections;

public class SnappyController1 : MonoBehaviour {
	
	public float moveSpeed;
	private float maxSpeed = 5f;
	
	private Vector3 input;
	
	
	void Update () {
		
	input = new Vector3(Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxis Vertical"));    		

     if (rigidbody.velocity.magnitude < maxSpeed) 
			
			
		{
			
			rigidbody.AddForce(input *moveSpeed);
			
		}
		
			
	}
	
}

While the code for the character controller for both objects is the same, they have different names, so that the controller on the sphere can be disabled when the “game” starts.

This is the code that I am using to disable the sphere controller when the “game” starts:

void Awake () {

		SnappyController1 otherScript = GetComponent<SnappyController1>();
		otherScript.enabled = false;
		
		}

In addition, the sphere has a “hit detection” script attached to it that destroys the cube when the two objects collide. The “hit detection” script is as follows:

using UnityEngine;
using System.Collections;

public class HitDetection : MonoBehaviour {

	

	
	// Update is called once per frame
	void OnCollisionEnter (Collision col) 
	{


		if (col.gameObject.name == "Cube") {
			
			
			Debug.Log ("It's finally working");
			}



		if (col.gameObject.name == "Cube") {

				
			Destroy(col.gameObject);
		
		
		}
			

		
	}	


}

I have been able to get the “hit detection” script to work with out any issue. However, when I attempt to alter the “hit detection” code to activate the character controller script on the sphere when it makes contact with the cube, the cube is no longer destroyed.

The updated version of the “hit detection” script looks like this:

using UnityEngine;
using System.Collections;

public class HitDetection : MonoBehaviour {

	

	
	// Update is called once per frame
	void OnCollisionEnter (Collision col) 
	{


		if (col.gameObject.name == "Cube") {
			
			
			Debug.Log ("It's finally working");
			}


		if (col.gameObject.name == "Cube") {


				SnappyController1 otherScript = GetComponent<SnappyController1>();
				otherScript.enabled = true;
				
			}


		if (col.gameObject.name == "Cube") {

				
			Destroy(col.gameObject);
				
		}
			
	}	

}

There is probably an easy way to fix this, but as I said I am new to both Unity and C#, so any help that you could offer would be greatly appreciated.

Thank you in advance.

I think your problem is that your GetComponent(); in the HitDetection class is operating on whatever you have the hitDetection Script attached to, which I don’t think is the same object where you successfully disable the otherScript in the awake function. This, or something like it, may be the solution

if (col.gameObject.name == "Cube") {          
    Debug.Log ("It's finally working");
    SnappyController1 otherScript = col.gameObject.GetComponent<SnappyController1>(); //This is the key change
    otherScript.enabled = true;
    Destroy(col.gameObject);
}

And I went ahead and got rid of your extra logic checks :slight_smile: