Hi, i am making a school project that is an interactive art space. My team and i went with a space theme. i am struggling to make a code that, when you click on the moon, stars appear one by one each time you click the moon. i have read and looked things up online and came up with nothing, or didn’t work. i would really appreciate some help, thank you.
That’s the right approach to anything related to code. Well done.
What didn’t work? Were there errors in the logs, did the game crash, or did a specific behavior not match your expectations? Provide the code you’ve tried so far, the reasons it didn’t work, and any console errors.
Here’s how to ask the question properly so we can help further. When asking a question, provide as much relevant information as possible:
- Share your entire relevant scripts in a code block rather than a screenshot:
```csharp
// your copy/pasted code here
```
- Include any errors or warnings you encountered, along with their exact messages.
- Explain what you were trying to achieve.
- Describe what actually happened versus what you expected. (Doesn’t work, is not a good description)
- List any troubleshooting steps you’ve already attempted.
- Provide steps to reproduce the problem, if possible.
This makes your question clearer and shows that you respect others’ time:
- Using a code block lets people easily copy and test your script instead of retyping it.
- Sharing the full script allows them to run it on their own machines.
- Providing the exact error message (if any) helps identify the issue more efficiently.
- Clearly stating your goal prevents guesswork about what you’re trying to accomplish.
- Describing what actually happened (rather than just what you expected) helps them reproduce the issue.
- Sharing what you’ve already tried prevents them from wasting time on the same solutions.
- Overall, clear and complete information saves everyone from playing “20 questions” just to understand your problem.
That sounds like some graphics and an animation to make the stars appear.
Your code would sense clicking on the moon and start the animation.
That’s it. That’s the entire problem space.
That’s not going to ever be useful. Work one small step at a time, like this:
Imphenzia: How Did I Learn To Make Games:
Two steps to tutorials and / or example code:
- do them perfectly, to the letter (zero typos, including punctuation and capitalization)
- stop and understand each step to understand what is going on.
If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.
Step #2 is particularly critical when learning.
If you are unwilling or unable to do Step #2, just ask someone else to do the whole game for you.
For instance, here’s all the steps necessary to click on something and make it go there. You need to build up to the ability to decompose a problem into all of these steps:
Interesting school project, @MoonAqua.
Let’s break down what you’re trying to accomplish in a couple of steps.
- Clicking the moon.
- Revealing a star when the moon is clicked.
The simplest way to accomplish this would be to start with a group of deactivated stars and then activate each one when the moon is clicked.

Set up
You’ll need a bunch of stars and a moon arranged in the scene accordingly. Place the stars and moon wherever you like.
Each star and the moon must have a collider component attached to them. It’ll be used later in the code.
You can optionally create a star prefab so changes on one star affect all the others, which saves you time when editing, such as updating each star’s material.
Ensure the moon isn’t obstructed by anything because it’s the one you’ll click.
Here’s a sample setup, where the white sphere is the moon, and the red cubes are the stars.
Next, ensure each star is deactivated by unchecking the active/inactive checkbox. Doing this “hides” the stars.
Scripting
Create an empty GameObject and attach a script to it. You can give the GameObject and script any name, but let’s go with GameManager for this example.
The GameManager script handles the click events on the moon and the revealing of stars.
public class GameManager: MonoBehaviour
{
void Update() { }
}
You’ll need to access each star in the Unity Editor, so create a _stars array field to store them. This example uses 10 stars, so this array is initialized to an empty array that can only hold 10 items.
You’ll also need an index (_starToActivateIndex) that will be updated as each star is revealed. Since you’re revealing stars from the beginning, this will be initialized to 0.
Here’s what the code looks like so far.
public class GameManager: MonoBehaviour
{
[SerializeField]
private GameObject[] _stars = new GameObject[10];
int _starToActivateIndex = 0;
void Update() { }
}
In the Update method, you’ll shoot a ray from the camera to the point you clicked using the main camera’s ScreenPointToRay method.
The next part checks if the player pressed the left mouse button (Input.GetMouseButtonDown(0)) and if the ray shot from the camera hit anything (Physics.Raycast(ray, out RaycastHit hitInfo, 1000)).
public class GameManager: MonoBehaviour
{
[SerializeField]
private GameObject[] _stars = new GameObject[10];
int _starToActivateIndex = 0;
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0) && Physics.Raycast(ray, out RaycastHit hitInfo, 1000))
{
Debug.Log($"You clicked on: {hitInfo.collider.name}");
}
}
}
You can test this out by logging to the console with the cubes enabled. Remember to deactivate them again after testing.

Now that clicking objects is working correctly, you can check if the object clicked is the moon (hitInfo.collider.name == "Sphere") and activate a star.
After each click, you move on to the next star by adding 1 to _starToActivateIndex.
Since you don’t want the index to exceed the number of stars in the array, you’ll add a check (_starToActivateIndex < _stars.Length) to ensure it’s always less than the total number of stars to prevent an error.
public class GameManager: MonoBehaviour
{
[SerializeField]
private GameObject[] _stars = new GameObject[10];
int _starToActivateIndex = 0;
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0) && Physics.Raycast(ray, out RaycastHit hitInfo, 1000))
{
if (hitInfo.collider.name == "Sphere" && _starToActivateIndex < _stars.Length)
{
_stars[_starToActivateIndex].SetActive(true);
_starToActivateIndex++;
}
}
}
}
Result

If you want, you can change the skybox, use an emissive material for the stars, and add some post-processing for a more polished look.

