0
\$\begingroup\$

I made a script which swaps two game objects on click.But the script won't work because the objects are actualy clones of the original prefab.

This is the script (UnityScript):

#pragma strict

var object1 : GameObject;
var object2 : GameObject;
function OnMouseDown ()
{

Instantiate(object2,object1.transform.position,object1.transform.rotation);

Destroy(object1);
}

I use this script to create other game objects (clones)[c#] :

using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour {

public GameObject[] obj;
public float spawnMin = 1f;
public float spawnMax = 2f;


// Use this for initialization
void Start () {
    Spawn ();
}

void Spawn() 
{
    Instantiate(obj[Random.Range(0, obj.GetLength(0))],transform.position, Quaternion.identity);
                    Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
                    }

}

The objects get renamed to NAME (Clone).

What I wanna do is make the script affect clones too.So they will swap when I click on them.

\$\endgroup\$
6
  • \$\begingroup\$ fyi: 1) Java is a separate language from JavaScript, and 2) the "JavaScript" in Unity isn't really the same as JavaScript in most places, thus it's more correctly referred to as UnityScript \$\endgroup\$
    – jhocking
    Jun 12 '14 at 19:03
  • \$\begingroup\$ Why does everyone in stackexchange care about little things,please let's focus on the REAL issue... \$\endgroup\$
    – Ssiro
    Jun 12 '14 at 19:09
  • 2
    \$\begingroup\$ If you think correctly identifying the programming language you're using is a little thing, then you probably will experience a lot of difficulties trying to understand a very technical field like game development. \$\endgroup\$
    – jhocking
    Jun 12 '14 at 19:33
  • \$\begingroup\$ Also, how does my posting a side comment imply that I'm not also thinking about the actual problem you've encountered? I was simply explaining to you why I made my edit. \$\endgroup\$
    – jhocking
    Jun 12 '14 at 19:35
  • 1
    \$\begingroup\$ Comments are not answers, they might include optional advises and corrections. We're programmers, we DO care about small things, since a semicolon can ruin our code. BTW you shouldn't mix C# and UnityScript both in single project. Choose one language for all of your code. \$\endgroup\$
    – noob
    Jun 14 '14 at 12:29
0
\$\begingroup\$

I didn't understand your problem exactly, but I guess you're having difficulty in running same script on different clones of a single prefab. If you've already attached a script to the prefab then it should work, or if you havn't, you can attach a script at runtime too.

I guess you just need to swap the position of two gameobjects, then you can use the following script -

public GameObject prefab1;
public GameObject prefab2;
GameObject object1;
GameObject object2;
void Spawn() //call this method whenever you want to instantiate the objects
{
    object1 = Instantiate(prefab1, position, Quarternion.identity) as GameObject; //position can be your initial position for first object
    object2 = Instantiate(prefab2, position, Quarternion.identity) as GameObject; //position can be your initial position for second object

    //You don't actually need to rename the clones, but if you want to, you can use object1.name to rename it.
    //If you need to attach scripts at runtime, you can use object1.AddComponent<ClassName>(); method for that.
}

void Swap() // call this method whenever you want to swap their position
{
    Vector3 pos1 = object1.transform.position;
    object1.transform.position = object2.transform.position;
    object1.transform.position = pos1;
}

This all can be done in single script, don't forget to attach prefabs in prefab1 and prefab2 from Inspector.

\$\endgroup\$
2
  • \$\begingroup\$ Your script declares variables "prefab1" and "prefab2" but then doesn't use those variables. I suspect you actually wanted the lines to say object1 = Instantiate(prefab1, position, Quarternion.identity) as GameObject; \$\endgroup\$
    – jhocking
    Jun 14 '14 at 14:40
  • \$\begingroup\$ @jhocking Thanks for noticing. I corrected the mistake. \$\endgroup\$
    – noob
    Jun 14 '14 at 15:19
1
\$\begingroup\$

Xerosigma's answer points out how you have to address this by using the object reference returned from Instantiate() but he didn't actually explain what you need to do.

The GameObject reference that you get back from the Instantiate() command then has to be passed to object1 (or object2, whichever you are trying to do) in the first script. I'm guessing you typically use those variables by dragging an object in the scene to the slot "object1" in the Inspector; however since the object you want to swap doesn't exist until the game is running, that object can't be referenced by that variable until after the game is running.

That means you can't drag the object in the editor manually; you'll have to set the variable in code. What I'm guessing you want to do is keep a reference to the swap script in the spawn script, and then pass in new objects that way. Something like:

public Swapper myswapper;

...

myswapper.object1 = Instantiate(etc.)
\$\endgroup\$
1
  • \$\begingroup\$ Small world Joseph, still waiting to hear back from Alex. I don't think he likes email. ;p \$\endgroup\$ Jun 13 '14 at 2:13
1
\$\begingroup\$

Instantiate returns the created GameObject. You should be able to capture it in a variable and change the name and more.

// UnityScript
var obj : GameObject;
obj = Instantiate( ... );  
obj.name = "Not A Clone!!!";

// C#
GameObject obj;
obj = Instantiate( ... ) as GameObject;  
obj.name = "Not A Clone!!!";
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I think you need to cast explicitly to GameObject in the C# version. \$\endgroup\$
    – kat0r
    Jun 14 '14 at 13:15
-1
\$\begingroup\$

I remove the Clone text from the name by adding this simple method to any of the components for the object being instantiated.

   void OnEnable()
   {
      name = name.Replace("(Clone)", "");
   }
\$\endgroup\$
1
  • 1
    \$\begingroup\$ His mention of the (Clone) name was a red herring, that doesn't actually have anything to do with the problem \$\endgroup\$
    – jhocking
    Jun 12 '14 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.