Orbiting bodies with unity 5.0

using UnityEngine;
using System.Collections;

public class Orbiting : MonoBehaviour {

    public Transform sun;
    public Transform planet;
    
   // public static 
    //public Rigidbody rb = UnityEngine.Component.GetComponent<Rigidbody>();
    void Awake()
    {
        Rigidbody rb;
        rb = GetComponent<Rigidbody>();
        planet = transform;
        
        rb.AddForce(transform.forward * 100);
        rb.AddForce(transform.up * 100);
    }
	// Use this for initialization
	void Start () {
        GameObject sun = GameObject.FindGameObjectWithTag("Sun");
        
        sun = sun.transform;

	}
	
	// Update is called once per frame
	void Update () {
       Rigidbody rb;
        rb = GetComponent<Rigidbody>();
        Vector3 line = sun.position - planet.position;
        line.Normalize();
        float distance = Vector3.Distance(sun.position, planet.position);
        rb.AddForce(line * 10 / distance);
	}
}

Okay, I’m trying to get two planetary bodies to orbit each other. I’m starting real small. Ultimately I hope to get a planet sim, where you can click and add either suns or planets and depending where you click they will either orbit, fall into, or leave the solar system. But I can’t seem to get this script to work. I followed a youtube tutorial online. However, this youtube video was made for 4.0ish version of unity. 5.0 has a bunch of changes I’m still trying to learn when using the tutorials. the planet and sun are prefabs, however, in the unity program I have the sun and planet marked with the relevant model in the program. IE sun to the sun transform and the planet to the planet transform. The program is not orbiting like it should, it actually isn’t orbiting at all, it is just sitting there, and I can’t seem to figure out why. I also did a search for this issue, but couldn’t find quite what I was looking for, so I’m going to try and ask, and hope I don’t get trolled to harshly. This script was written for 5.0, just in case I wasn’t clear.

NOTE****

Figured out what was going on. I had marked the objects with ‘is Kinematic’ and that what was causing them to not respond to the orbiting script. I don’t know why that was, but that what was happening. Thank you all for your help. Feel free to add suggestions.

Not sure if you really need physics, but a simple orbit can be done without using the physics system.

public class Orbit : MonoBehaviour {

	public Transform OrbitPivot;   // The transform that this object will orbit around
	public float OrbitSpeed = 100f;
	
	// Update is called once per frame
	void Update () {
		this.transform.RotateAround(OrbitPivot.position, Vector3.up, OrbitSpeed * Time.deltaTime);
	}
}

void Start () {
GameObject sun = GameObject.FindGameObjectWithTag(“Sun”);
sun = sun.transform;
}

This part of your code is problematic. You’ve declared a local variable called sun, which has the same name as your Orbiting class variable called sun. This in itself isn’t bad, but when you have sun = sun.transform in your Start method you’re just assigning to your local variable sun. I’m surprised that actually compiles since you’re assigning a Transform to a GameObject. But maybe Unity has an implicit cast for that?

To fix this you need to either rename your local variable (probably the best option). Or you can assign to the class variable like this:

this.sun = sun.transform

This tell’s Unity that you’re referring to the object instance scope, so it will look for the sun variable there instead of the local one you’ve created.

There may be other issues, but that’s what jumped out at me.