Skip to main content
Fixed code block
Source Link
Vaillancourt
  • 16.4k
  • 17
  • 56
  • 61

I'm wondering if there is a way to have an Object when duplicated in the editor assign a GameObject variable to the newly duplicated version of itself. Is there an option in Unity or do I have to code it out.

So I would like it to follow these steps:

1: So I would have the old GameObject with the variable named "nextStop" with nothing assigned to it.

2: Then I duplicate said object and the older object's variable is assigned to the newly duplicated object.

3: Done.

  1. So I would have the old GameObject with the variable named "nextStop" with nothing assigned to it.
  2. Then I duplicate said object and the older object's variable is assigned to the newly duplicated object.
  3. Done.

[![enter image description here][1]][1]

I'm doing this for a rail editor to make it easier to create new rails to follow.

[![enter image description here][2]][2]

I would like not to have to assign to each and every Object it will make things so much easier to explain for others to use. "Just duplicate these objects."

And in case you need it and Unity doesn't already have an option built in here's my code for the track.

using UnityEngine;
using System.Collections;

public class Rails : MonoBehaviour {

    public GameObject nextStop;
    LineRenderer myLine;
    public bool endLevel = false;
    public Train myShip;
    void OnDrawGizmos()
    {
        if (nextStop) {
            Gizmos.color = Color.green;
            Gizmos.DrawLine(transform.position, nextStop.transform.position);
        }
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "train")
        {
            if (nextStop)
            myShip.track = nextStop;
            if (endLevel)
            {
                // Load the next level
            }
        }
    }
}

Oh yeah I should mention I have little experience modifying code to do things in the editor. [1]: https://i.sstatic.net/YDeJm.png [2]: https://i.sstatic.net/LqtG7.png

I'm wondering if there is a way to have an Object when duplicated in the editor assign a GameObject variable to the newly duplicated version of itself. Is there an option in Unity or do I have to code it out.

So I would like it to follow these steps:

1: So I would have the old GameObject with the variable named "nextStop" with nothing assigned to it.

2: Then I duplicate said object and the older object's variable is assigned to the newly duplicated object.

3: Done.

[![enter image description here][1]][1]

I'm doing this for a rail editor to make it easier to create new rails to follow.

[![enter image description here][2]][2]

I would like not to have to assign to each and every Object it will make things so much easier to explain for others to use. "Just duplicate these objects."

And in case you need it and Unity doesn't already have an option built in here's my code for the track.

using UnityEngine;
using System.Collections;

public class Rails : MonoBehaviour {

    public GameObject nextStop;
    LineRenderer myLine;
    public bool endLevel = false;
    public Train myShip;
    void OnDrawGizmos()
    {
        if (nextStop) {
            Gizmos.color = Color.green;
            Gizmos.DrawLine(transform.position, nextStop.transform.position);
        }
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "train")
        {
            if (nextStop)
            myShip.track = nextStop;
            if (endLevel)
            {
                // Load the next level
            }
        }
    }
}

Oh yeah I should mention I have little experience modifying code to do things in the editor. [1]: https://i.sstatic.net/YDeJm.png [2]: https://i.sstatic.net/LqtG7.png

I'm wondering if there is a way to have an Object when duplicated in the editor assign a GameObject variable to the newly duplicated version of itself. Is there an option in Unity or do I have to code it out.

So I would like it to follow these steps:

  1. So I would have the old GameObject with the variable named "nextStop" with nothing assigned to it.
  2. Then I duplicate said object and the older object's variable is assigned to the newly duplicated object.
  3. Done.

[![enter image description here][1]][1]

I'm doing this for a rail editor to make it easier to create new rails to follow.

[![enter image description here][2]][2]

I would like not to have to assign to each and every Object it will make things so much easier to explain for others to use. "Just duplicate these objects."

And in case you need it and Unity doesn't already have an option built in here's my code for the track.

using UnityEngine;
using System.Collections;

public class Rails : MonoBehaviour {

    public GameObject nextStop;
    LineRenderer myLine;
    public bool endLevel = false;
    public Train myShip;
    void OnDrawGizmos()
    {
        if (nextStop) {
            Gizmos.color = Color.green;
            Gizmos.DrawLine(transform.position, nextStop.transform.position);
        }
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "train")
        {
            if (nextStop)
            myShip.track = nextStop;
            if (endLevel)
            {
                // Load the next level
            }
        }
    }
}

Oh yeah I should mention I have little experience modifying code to do things in the editor. [1]: https://i.sstatic.net/YDeJm.png [2]: https://i.sstatic.net/LqtG7.png

I'm wondering if there is a way to have an Object when duplicated in the editor assign a GameObject variable to the newly duplicated version of itself. Is there an option in Unity or do I have to code it out.

So I would like it to follow these steps:

1: So I would have the old GameObject with the variable named "nextStop" with nothing assigned to it.

2: Then I duplicate said object and the older object's variable is assigned to the newly duplicated object.

3: Done.

[![enter image description here][1]][1]

I'm doing this for a rail editor to make it easier to create new rails to follow.

[![enter image description here][2]][2]

I would like not to have to assign to each and every Object it will make things so much easier to explain for others to use. "Just duplicate these objects."

And in case you need it and Unity doesn't already have an option built in here's my code for the track.

using UnityEngine;

using System.Collections;

public class Rails : MonoBehaviour {

using System.Collections;

public class Rails : MonoBehaviour {

    public GameObject nextStop;
    LineRenderer myLine;
    public bool endLevel = false;
    public Train myShip;
    void OnDrawGizmos()
    {
        if (nextStop) {
            Gizmos.color = Color.green;
            Gizmos.DrawLine(transform.position, nextStop.transform.position);
        }
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "train")
        {
            if (nextStop)
            myShip.track = nextStop;
            if (endLevel)
            {
                // Load the next level
            }
        }
    }
}

}

Oh yeah I should mention I have little experience modifying code to do things in the editor. [1]: https://i.sstatic.net/YDeJm.png [2]: https://i.sstatic.net/LqtG7.png

I'm wondering if there is a way to have an Object when duplicated in the editor assign a GameObject variable to the newly duplicated version of itself. Is there an option in Unity or do I have to code it out.

So I would like it to follow these steps:

1: So I would have the old GameObject with the variable named "nextStop" with nothing assigned to it.

2: Then I duplicate said object and the older object's variable is assigned to the newly duplicated object.

3: Done.

[![enter image description here][1]][1]

I'm doing this for a rail editor to make it easier to create new rails to follow.

[![enter image description here][2]][2]

I would like not to have to assign to each and every Object it will make things so much easier to explain for others to use. "Just duplicate these objects."

And in case you need it and Unity doesn't already have an option built in here's my code for the track.

using UnityEngine;

using System.Collections;

public class Rails : MonoBehaviour {

public GameObject nextStop;
LineRenderer myLine;
public bool endLevel = false;
public Train myShip;
void OnDrawGizmos()
{
    if (nextStop) {
        Gizmos.color = Color.green;
        Gizmos.DrawLine(transform.position, nextStop.transform.position);
    }
}
void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "train")
    {
        if (nextStop)
        myShip.track = nextStop;
        if (endLevel)
        {
            // Load the next level
        }
    }
}

}

Oh yeah I should mention I have little experience modifying code to do things in the editor. [1]: https://i.sstatic.net/YDeJm.png [2]: https://i.sstatic.net/LqtG7.png

I'm wondering if there is a way to have an Object when duplicated in the editor assign a GameObject variable to the newly duplicated version of itself. Is there an option in Unity or do I have to code it out.

So I would like it to follow these steps:

1: So I would have the old GameObject with the variable named "nextStop" with nothing assigned to it.

2: Then I duplicate said object and the older object's variable is assigned to the newly duplicated object.

3: Done.

[![enter image description here][1]][1]

I'm doing this for a rail editor to make it easier to create new rails to follow.

[![enter image description here][2]][2]

I would like not to have to assign to each and every Object it will make things so much easier to explain for others to use. "Just duplicate these objects."

And in case you need it and Unity doesn't already have an option built in here's my code for the track.

using UnityEngine;
using System.Collections;

public class Rails : MonoBehaviour {

    public GameObject nextStop;
    LineRenderer myLine;
    public bool endLevel = false;
    public Train myShip;
    void OnDrawGizmos()
    {
        if (nextStop) {
            Gizmos.color = Color.green;
            Gizmos.DrawLine(transform.position, nextStop.transform.position);
        }
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "train")
        {
            if (nextStop)
            myShip.track = nextStop;
            if (endLevel)
            {
                // Load the next level
            }
        }
    }
}

Oh yeah I should mention I have little experience modifying code to do things in the editor. [1]: https://i.sstatic.net/YDeJm.png [2]: https://i.sstatic.net/LqtG7.png

Source Link
Ryan Henry
  • 357
  • 2
  • 4
  • 13

Assigning Objects when duplicated in the editor?

I'm wondering if there is a way to have an Object when duplicated in the editor assign a GameObject variable to the newly duplicated version of itself. Is there an option in Unity or do I have to code it out.

So I would like it to follow these steps:

1: So I would have the old GameObject with the variable named "nextStop" with nothing assigned to it.

2: Then I duplicate said object and the older object's variable is assigned to the newly duplicated object.

3: Done.

[![enter image description here][1]][1]

I'm doing this for a rail editor to make it easier to create new rails to follow.

[![enter image description here][2]][2]

I would like not to have to assign to each and every Object it will make things so much easier to explain for others to use. "Just duplicate these objects."

And in case you need it and Unity doesn't already have an option built in here's my code for the track.

using UnityEngine;

using System.Collections;

public class Rails : MonoBehaviour {

public GameObject nextStop;
LineRenderer myLine;
public bool endLevel = false;
public Train myShip;
void OnDrawGizmos()
{
    if (nextStop) {
        Gizmos.color = Color.green;
        Gizmos.DrawLine(transform.position, nextStop.transform.position);
    }
}
void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "train")
    {
        if (nextStop)
        myShip.track = nextStop;
        if (endLevel)
        {
            // Load the next level
        }
    }
}

}

Oh yeah I should mention I have little experience modifying code to do things in the editor. [1]: https://i.sstatic.net/YDeJm.png [2]: https://i.sstatic.net/LqtG7.png