Correct way to access a script-created component?

With no help I finally figured this out. Just in case someone else needs the info I’m going to leave the post with my current code up as an example. If anyone has comments I’d love to see them.


I have an asteroid. I have a ship. The ship shoots a torpedo at the asteroid. If the asteroid is hit by a torpedo, the asteroid should create a spring joint attached to the ship.

Am I doing this right? How do I set the spring joint’s connectedBody to be the ship? How should I set the other variables for the spring joint? There will be many asteroids so I want to make sure the correct one is connected which is why I’m putting the script on the asteroid instead of the ship.

After thinking and searching everywhere for some information on this I wanted to clarify a bit. The code I have adds a joint, it works. The problem is that Unity tells me “An instance of type ‘UnityEngine.Joint’ is required to access non static member ‘connectedBody’.”

After I’ve created the joint, how do I access it? How do I get the editor to stop telling me that because there is no joint (YET) I can’t access it?

The following code is attached to the asteroid.

var currentPlayer : GameObject;
var myJoint : Joint;
function Start()
	{
		// Find our player's object for later use.
		currentPlayer = GameObject.FindWithTag("Player");
	}

function Update () 
	{
		// It's a 2d game from the top down, I don't want physics messing 
		// with my objects y.
		// Is there a way to have a global y clamp to 0?
		transform.position.y = 0;
	}

// when there is a collision...
function OnCollisionEnter( collision : Collision)
{
	// if the collision is with a torpedo...
	if(collision.gameObject.tag == "Torpedo")
	{
		//Make sure there isn't a springjoint already, otherwise Unity gets angry.
		if(GetComponent(SpringJoint) == null)
		{
			// Add a shiny new SpringJoint, might look into the custom joint later
			gameObject.AddComponent(SpringJoint);
		}
		
		// Check again to make sure there IS a springjoint this time.  I should put 
// this in an else.
		if(GetComponent(SpringJoint) != null)
		{
			// Not sure I need to do this but I'm so happy it works I'm
			// not changing it right now.  Get the springjoint I just created
			// and pop it into myJoint.
			myJoint = GetComponent(SpringJoint);
			// Finally, set the connection between the asteroid and the player ship
			// next I'll change the other joint variables to get what I want.
			myJoint.connectedBody = currentPlayer.rigidbody;
		}
	}
}

Thanks for leaving this up and commenting the code, helped me sort a similar problem.

Just fyi you can use

GetComponent(SpringJoint)

on its own instead of explicitly checking whether it’s null with

GetComponent(SpringJoint) != null

Naturally the same applies to “does not exist” checks as well. :slight_smile:

Why do people post and then I only ever check the latest post date, not the OP >_>

When you AddComponent, keep a reference to it so you can fiddle with it.

SpringJoint sj = gameObject.AddComponent(SpringJoint);
sj.connectedBody = currentPlayer.rigidbody;

Edit: wow old thread. doh