- Home /
detecting whether the object has been clicked in another script
Currently, I have an object A (attached with script A), the function of script A is that when user click on the scene, object A will appear. Object A can be clicked multiple times, depends on the user.
There will be another object called object B. When object A appears on the scene(after clicking), object B will appear next to object A.
What I want to achieve:
know that the object A has been clicked within script A
tell object B to appear on the scene
tell object B to appear right next to object A
So I only write a little bit on script B as I don't know where to start.... but script A is completed
public class object_B : MonoBehaviour {
public Renderer rend;
// Use this for initialization
void Start () {
rend = this.GetComponent<Renderer> ();
// make the object invisible
rend.enabled = false;
}
// Update is called once per frame
void Update () {
}
}
Thanks a lot guys! Actually, I end up with Instantiating object B within object A's script, so sth like this: GameObject right = (GameObject)Instantiate(object_B, transform.position + (transform.right), transform.rotation );
Also need to create the the object B before instantiating it!
Answer by nj4242 · Sep 02, 2016 at 10:49 AM
You don't need to add another script for object B, Just adjust the object B next to object A as you like from the Scene, also disable it from the Inspector (you will able to do it from a checkbox beside your object name in the inspector or you can simply do it within the script itself, just write : GameObject.Find ("object_B_name").active = false;
in the Start ( ) function) . Add a collider to Object A.
Also add this code to script :
void OnMouseDown()
{
GameObject.Find ("object_B_name").active = true;
}
Attach the script and collider to A only, no need to add anything to B.
Answer by b1gry4n · Sep 02, 2016 at 10:03 AM
Sounds like you should have some sort of "manager" script. Have the manager spawn in object A and object B. If object A or object B are clicked, it reports the click back to the manager. From there you can do whatever. When the objects are spawned in, access the script and make a reference to the manager.
On the objects you could have something like this...
public class AnObject : MonoBehaviour
{
public enum ObjectType
{
objectA,
objectB
}
public ObjectType typeOfObject;
public ObjectManager manager;
//called by the objectmanager to set this object up
public void Setup(ObjectManager o, ObjectType ot)
{
typeOfObject = ot;
manager = o;
}
//when this object is clicked, call this
public void Clicked()
{
manager.Clicked(this);
}
}
And your manager could be:
public class ObjectManager : MonoBehaviour
{
public GameObject objectPfb;
void Start()
{
SpawnObject(Vector3.zero, AnObject.ObjectType.objectA);
}
//spawns an object at the pos as the type
void SpawnObject(Vector3 pos, AnObject.ObjectType ot)
{
//instantiate the prefab of our object
GameObject g = (GameObject)Instantiate(objectPfb, pos, Quaternion.identity);
//setup the object
g.GetComponent<AnObject>().Setup(this, ot);
}
//called by an object when that object is clicked
public void Clicked(AnObject objClicked)
{
//do whatever you want with this object
if (objClicked.ObjectType == AnObject.ObjectType.objectA)
{
SpawnObject(new Vector3(objClicked.transform.position.x + 1, objClicked.transform.position.y, objClicked.transform.position.z), AnObject.ObjectType.objectB);
}
else if (objClicked.ObjectType == AnObject.ObjectType.objectB)
{
//whatever you want with object B
}
}
}
Your answer
Follow this Question
Related Questions
detect if i'm looking at specific object 1 Answer
if object is destroyed 2 Answers
How to shoot a bullet based on another object's position? 1 Answer
Detect radius from object 1 Answer