- Home /
How do I hold an object within one of my spheres, to where I can click the sphere and activate the object?
What I have right now is a primary sphere called the IncomeSphere, and on the OnMouseDown() function of that IncomeSphere there are 4 identical spheres that are instantiated called ActionSpheres. Within the OnMouseOver() of both the IncomeSphere and the ActionSpheres they fade in/out identifying which one is selected. On MouseUp() the ActionSpheres are destroyed. I want there to be 4 individual items within each of the ActionSpheres when they instantiate.
There are 100 gameboard like plate objects that are instantiated at the beginning of the level in the form of a 10x10 field (They have the same fade in/out highlight function). Overall I want an object from one of those 4 ActionSpheres to be activated and instantiate in the OnMouseUp() function of those plates. using UnityEngine; using System.Collections; using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class IncomeSphere : MonoBehaviour {
//Constants for location
public const float _offSetY = 58.0f;
public const float _offSetZ = 166.0f;
public const int _primarySphere = 296;
//Constant for the slots unlocked
public const int _slotsAvailable = 4;
//Sets the Actoin Sphere prefab
public GameObject actionSphere;
//Creates three arrays to instantiate the Action Spheres
//int[] _aSlot (actionSlot) to adjust the PrimarySphere(X) value
//Vector3[] _aSpherePos (actionSpherePosition) to reapply the vectors
//GameObject[] _aSphere (actionSphere) to hold the instantiated sphere
private GameObject[] _aSphere = new GameObject[_slotsAvailable];
private Vector3[] _aSpherePos = new Vector3[_slotsAvailable];
private int[] _aSlot = new int[_slotsAvailable];
//Sets the transparency state of the Action Spheres
private bool alphaState = false;
// Use this for initialization
void Start() {
//Recalculates the vectors of each Action Sphere
for (int cnt = 0; cnt < _slotsAvailable; cnt++) {
_aSlot[cnt] = _primarySphere + (cnt + 1);
_aSpherePos[cnt] = new Vector3(_aSlot[cnt], _offSetY, _offSetZ);
//Debug.Log(_aSpherePos[cnt] + " is the position for slot " + cnt);
}
renderer.material.color = new Color(0, .4f, .4f, .14f);
//Debug.Log ("Color is now " + renderer.material.color);
}
// Update is called once per frame
void Update (){
}
void OnMouseOver() {
#region Fade In
//If the alpha's state is set to false and rendered below a float of 0.84
if (alphaState == false) {
if (renderer.material.color.a < .84f) {
//Then increase the alpha by framerate * 2 and print progress
renderer.material.color += new Color(0, 0, 0, Time.deltaTime * 2);
//Unless the alpha is rendered at a float of 0.84
} else {
//Then set the alpha's state to true
alphaState = true;
}
}
#endregion
#region Fade Out
//If the alpha's state is set to false and is rendered above a float of 0.14
if (alphaState == true) {
if (renderer.material.color.a > .14f) {
//Then decrease the alpha by framerate * 2 and print progress
renderer.material.color -= new Color(0, 0, 0, Time.deltaTime * 2);
//Unless the alpha is rendered at a float of 0.14
} else {
//Then set the alpha's state to false
alphaState = false;
}
}
#endregion
}
void OnMouseDown() {
#region Creates the Action Spheres
//Assigns space for Action Spheres
//Creates the Action Spheres
for(int cnt = 0; cnt < _slotsAvailable; cnt++) {
Debug.Log(_aSpherePos[cnt] + " is the position for slot " + (cnt + 1));
_aSphere[cnt] = Instantiate(actionSphere, _aSpherePos[cnt], Quaternion.identity)as GameObject;
}
#endregion
}
void OnMouseUp() {
//Creates a sphere for an action to be set
for (int cnt = 0; cnt < _slotsAvailable; cnt++) {
Debug.Log("Slot " + (cnt + 1) + " has been destroyed");
Destroy(_aSphere[cnt]);
}
}
//If the mouse exits the game object
void OnMouseExit() {
//Then revert the rendering to it's original float
renderer.material.color = new Color(0, .4f, .4f, .14f);
Debug.Log ("Color reset to " + renderer.material.color);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613113226im_/https://answers.unity.com/themes/thub/images/avi.jpg)