Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I tried this to get the score in my game and I tried this:
foreach (stella stella in stelle)
if (stella.rettangolo.Intersects(giocatore.rect))
{
score=score+10;
}
It functions but not well because if I stay a long time near the star the score continues to inrement even if it is not visible. how can i fix it?
You can add an extra flag that tells you whether or not the star has been picked up. This must be a field in the stella class. Let's call it...
public bool captata; //I'll go with some Google translated Italian.
When the level starts, initialize it with the value false:
foreach (stella stella in stelle)
{
stella.captata = false;
}
When the player intersects the star, you set the flag appropriately. At the same time, you only check for collision if the star hasn't been picked up yet.
foreach (stella stella in stelle)
if ((!stella.captata) && (stella.rettangolo.Intersects(giocatore.rect)))
{
score=score+10;
stella.captata = true;
}
Another way would be to remove the star from the stars collection completely.
for (int i = stelle.Count - 1; i >= 0; --i)
{
if (stelle[i].rettangolo.Intersects(giocatore.rect))
{
score = score + 10;
stelle.RemoveAt(i);
}
}
You need to change your game logic so that you either remove the "star" from the "stars" collection once a player comes near it, or so that you mark it as touched by the player.
Depending on what the logic of the game is, you can also only have the player get points when they start touching the star, but not while they are still touching it. you can have a bool, isTouchingStar and set it to true if you detect the player touching it.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I am making a game in Unity, 2D and for the creation of levels, I have added a for loop so that a number of blocks is generated a certain number of times. The problem is that it never ends, that is, instead of ending on the game screen when the two blocks are created, it continues to generate blocks infinitely.
public void GenerateInitialBlocks()
{
for (int i = 0; i < 2; i++)
{
AddLevelBlock();
}
}
I have reset the Script in Unity because it usually gives compilation errors or crashes, but it still doesn't work. Thanks for read.
I believe the problem could be in how many times the method is called.
find where in the code the method is called (perhaps using search)
make sure it's called only once per level and not repeatedly when refreshing or repainting (scene updating methods).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Recently I have been getting into Unity and have started making my first game. I am really happy with it but recently I have been getting really annoyed.
I have made a level system in my game (once you complete the first level (all level are separate scenes)you move on to the next level). I have found out how to use application.loadlevel, etc. But I also want the level menu (where you can select your past levels or current one by clicking a button) to turn intractability on once you have completed the previous level by picking up a cube. Unfortunately I don't have any idea how to do it as all my scripts have failed.
Please help me and thanks in advance and I am a beginner so don't explain things too advanced. Just tell me what to do and what I need to write in my scripts. If I have to use prefabs please tell me how as that confuses me as well.
First, you need to save somewhere that you have completed a level. This information must be saved in a persistent manner, otherwise, your player will have to restart your whole game each time he launches your game. There are many ways to do so, but PlayerPrefs could be a starting point.
Once any level is completed (before loading the next scene), call the following function :
public void OnLevelCompleted()
{
// Retrieve name of current scene / level
string sceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
PlayerPrefs.SetInt( sceneName, 1 ) ; // Indicates the level is completed
}
Then, in your home scene, attach a script to your buttons with the following piece of code :
public string SceneName ; // Indicate which level this button must load once you click on it. Be carefull, the name must be the same as in your Build Settings
protected void Awake()
{
UnityEngine.UI.Button button = GetComponent<UnityEngine.UI.Button>();
if( button != null )
{
// Make the button load the given scene
button.onClick.AddListener( () => UnityEngine.SceneManagement.SceneManager.LoadScene( SceneName ) ) ;
// Make the button interactable only if the given scene / level has been completed
button.interactable = PlayerPrefs.GetInt( SceneName ) > 0 ;
}
else
Debug.LogWarning("No button component attached", this ) ;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am making a robot battling game where I want the enemy to randomly move and then sometimes travel towards the enemy. Code that I want the movement to be in.
else if (avoid == false)
{
transform.LookAt(target);
transform.Translate(Vector3.forward * Time.deltaTime * movementSpeed);
currentLerpTime = 0;
}
This code just makes the AI move towards the player but I also want it occasionally to move in a random direction and then change direction occasionally. How would I do this?
To make it move in a random direction, the variable you need to edit is the vector.
else if (avoid == false)
{
transform.LookAt(target);
transform.Translate(Vector3.forward*Time.deltaTime*Speed);
// ^^^^^^
currentLerpTime = 0;
}
The problem here though is while it moves it will continue to keep looking at the target (i am assuming this is getting called each frame). In terms of generating three random numbers, you can do this by yourself with just C# or go read this
https://docs.unity3d.com/ScriptReference/Random.html
That link should really help you out.
Hope this helps.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So Im trying to write this code where the character's passive skill gets interrupted when he is hit. The way I want to do that is by having the program grab the characters health value from the health variable and check against it every frame to see if its dropped.
However, after researching I cant seem to find a way to actually do this. Grab a number from the variable without the new variable being effected by it.
Any ideas?
During each frame use another variable to store the last known value of the health variable, then compare the variables to see if they have different values. For example:
int health = 100;
int lastFrameHealth;
void Start ()
{
lastFrameHealth = health;
}
void Update ()
{
if (health < lastFrameHealth)
{
StartCoroutine("InterruptPassiveSkill", 5f);
}
lastFramehealth = health;
}
void OnCollisionEnter(Collision collision)
{
health -= 10;
}
IEnumerator InterruptPassiveSkill (float seconds)
{
// insert code to deactivate passive skill here:
yield return new WaitForSeconds(seconds); // delay for number of seconds
// insert code to reactivate passive skill here:
}
The coroutine used in this example is a nice, easy way to add a delay. Coroutines also make simple state machines much quicker to create. Coroutines are a Unity feature, non-native to C#.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a list full of game objects I'm trying to iterate through and spawn one at a time. When a new game object spawns, I'm wanting the old one to disappear. However, at the moment my code is spawning my entire gameobject list as opposed to just the one element.
Can someone please take a look over my code to see what I'm doing wrong?
if(GUI.Button(new Rect(10,130,100,50), "Forward"))
{
for( int i = 0; i < object_List.Count; i++)
{
Instantiate((GameObject)object_List[i]);
}
}
If I understand you correctly, you're trying to instance an object every time you press the button, while at the same time deleting the currently spawned object.
Create a private scoped variable to store your current index:
private int index = 0;
Then you can go ahead and instance on object at a time:
if(GUI.Button(new Rect(10,130,100,50), "Forward"))
{
if (index > 0 && object_List[index] != null)
{
Destroy((GameObject)object_List[index]);
}
Instantiate((GameObject)object_List[index]);
index ++;
}
You just need to do some checking whether index is actually larger than your list, so you won't get an IndexOutOfBounds Exception.