Simple Integer C# Unity i=i+1 not adding - c#

so i'm trying to do a little game on unity . And i did a script , when you press left click on an object , it open a canvas and another one and then it add +1 to i , like that when i found every object . (so when i=3) it shows the end screen. But when i go in game mode , when i press left click it doesnt add the number , like every object have his same i , but i dont understand they have the same function no ?
And sometimes 2 of 3 works, i dont know if thats clear to understand.
using UnityEngine;
using UnityEngine.UI;
public class ActionObjet : MonoBehaviour
{
public Image image1;
public Image image2;
public Image image3;
public int i = 0;
void Start()
{
image1.enabled = false;
image2.enabled = false;
image3.enabled = false;
}
void Update()
{
if (Input.GetKeyDown("space"))
{
if (image1.enabled == true)
{
i = i + 1;
print(i);
image2.enabled = !image2.enabled;
image1.enabled = !image1.enabled;
FindObjectOfType<AudioManager>().Play("bruit_recuperer_objet");
}
}
}
void OnMouseDown()
{
if (image1.enabled == false)
{
image1.enabled = !image1.enabled;
}
}
}
https://i.stack.imgur.com/sOSJH.png

So I found the solution , I'm a bit sad that I wasted 5 hours just for that but yea..
I just changed the
public int i=0
into
static int i=0;
This is because each gameObject with this script has its own instance of all the variables. A static variable's value is universal to all instances of the same class so any one of them can update it for all of them when marked static.

Related

How do I prevent UI button from resetting to start condition on every click? Unity3D

The answer to the boolean issue provided by #Mario below solved the main issue as well
The code in the original post (below the line) had an unrelated problem ('==' instead of '=') but in fixing that I've realized that I am having an issue with the string name of 'cubeRend.material' which includes the additive Instance after it and therefore doesn't seem to be counted as logically equal to the string name of 'material[0].'
I don't THINK that is the issue behind the resetting problem however, because I did find a question about a similar resetting problem on the Unity answer forum here: https://answers.unity.com/questions/1303925/ui-buttons-images-resets-after-scene-reloads-scrip.html
Unfortunately no one provided any responses to that question which I could try to apply in this case. I will try to sort out my equivalency problem and then update with the improved code
I'm trying to make a UI button change the colors on a cube on every click. The materials are in an array. In the start function, I set the initial condition of the cube's renderer to material[0]. In the ChangeCubeColor function (which is referred to on the UI button's inspector), I use a simple if/else statement to check which material is currently assigned to the cube. On clicking the button, Unity seems to reset the material back to the original condition and then invisible to the eye follow the if/else instructions to set the color to the 2nd color in the array. The affect is that on the first time you play, the button will change the color, but every time after that, the color is stuck on the second color.
Here is my code. I apologize for all of the debug statements. I was trying to figure out when the state was or was not changing. I've also included a screenshot of my console upon first play and the first 3 clicks of the button. Lastly, the code with the debug statements removed for clarity.
Thanks in advance for any ideas.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeCubeColor : MonoBehaviour
{
public Material[] material;
Renderer cubeRend;
void Start()
{
cubeRend = GetComponent<Renderer>();
cubeRend.material = material[0];
Debug.Log(cubeRend.material);
}
public void CubeColorChange()
{
if(cubeRend.material = material[0])
{
Debug.Log("cubeRend.material = material[0]");
cubeRend.material = material[1];
Debug.Log("Make 1: "+cubeRend.material);
Debug.Log("cubeRend.material = material[1]");
}
else if (cubeRend.material = material[1])
{
Debug.Log("cubeRend.material = material[1]");
cubeRend.material = material[0];
Debug.Log("Make 0: " + cubeRend.material);
Debug.Log("cubeRend.material = material[0]");
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeCubeColor : MonoBehaviour
{
public Material[] material;
Renderer cubeRend;
void Start()
{
cubeRend = GetComponent<Renderer>();
cubeRend.material = material[0];
}
public void CubeColorChange()
{
if(cubeRend.material = material[0])
{
cubeRend.material = material[1];
}
else if (cubeRend.material = material[1])
{
cubeRend.material = material[0];
}
}
}
Try this way. This work for 2 materials if you want more you can use an int index and update the index without use if for each material
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeCubeColor : MonoBehaviour
{
public Material[] material;
public bool state;
Renderer cubeRend;
void Start()
{
cubeRend = GetComponent<Renderer>();
cubeRend.material = material[0];
//state = false is material 0 //state = true is material 1
state = false;
}
public void CubeColorChange()
{
//Change the state to the other state
state = !state;
cubeRend.material = (state) ? material[1] : material[0];
}
}
EDIT 1:
Here is the variation with index for more materials
using UnityEngine;
public class ChangeCubeColor : MonoBehaviour
{
public Material[] material;
public int index;
Renderer cubeRend;
void Start()
{
cubeRend = GetComponent<Renderer>();
index = 0;
cubeRend.material = material[index];
}
public void CubeColorChange()
{
//Increase index
index = (material.Length - 1 > index) ? index + 1 : 0;
cubeRend.material = material[index];
}
}

One script changing multiple UI images, but it's not working

I'm having trouble with this code I'm trying to make.
My goal: Make each level button have that level's rank on it as well,
but I'm trying to do it from one script.
Firstly, here's how I have set up everything:
Those are the ranks inside each button and with a Rankdictator script that I made inside each rank gameobject.
Here's the script for the Rankdictator:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Rankdictator : MonoBehaviour
{
int levelnumberrank;
private Image thisimage;
public Sprite rankgold;
public Sprite ranksilver;
public Sprite rankbronze;
public Sprite blank;
private Levellockcheck levellockcheck;
public void Start()
{
levellockcheck = FindObjectOfType<Levellockcheck>();
thisimage = this.GetComponent<Image>();
levelnumberrank = PlayerPrefs.GetInt(("rankoflevel") + (levellockcheck.thisbuttonlevel));
if(levelnumberrank == 3)
{
thisimage.sprite = rankgold;
}
else if(levelnumberrank == 2)
{
thisimage.sprite = ranksilver;
}
else if (levelnumberrank == 1)
{
thisimage.sprite = rankbronze;
}
else if (levelnumberrank == 0)
{
thisimage.sprite = blank;
}
}
}
Now here's the problem. As I said, the ranks don't show on each button correctly when there are multiple buttons active, however, when I only have one button active, the rank shows. Here's an example:
That is when I have both buttons active. (Level 1 should have full rank and level 2 should have low rank. But it's showing nothing)
Now here's when only one button is active. It shows correctly.
Edit
I already used Debug.Log to confirm the levelnumberrank on each button and it references as expected.
But, it still doesn't reference the rank on each button when all of them are active, only does so when one button is active while the others are inactive.
I figured the problem out: it was the levellockcheck!
On each button, there was a levellockcheck script with a public int thisbuttonlevel.
I was, on each Rankdictator, referencing the levellockcheck like so:
Levellockcheck = FindObjectofType<levellockcheck>();
levelnumberrank = PlayerPrefs.GetInt(("rankoflevel") + (levellockcheck.thisbuttonlevel));
By doing that, I'm referencing different numbers all at the same time from each single button. Because the (thisbuttonlevel) on each button was different.
What I was supposed to do on Rankdictator:
Levellockcheck = this.GetComponentInParent<levellockcheck>();
By doing that, I referenced the (thisbuttonlevel) of the button itself.
And now:
It works!

How can i use enum in one script and another script?

In the first script:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightsEffects : MonoBehaviour
{
public enum Directions
{
Forward, Backward
};
private Renderer[] renderers;
private float lastChangeTime;
private int greenIndex = 0;
public void LightsEffect(List<GameObject> objects, Color instantiateColor, Color colorEffect)
{
renderers = new Renderer[objects.Count];
for (int i = 0; i < renderers.Length; i++)
{
renderers[i] = objects[i].GetComponent<Renderer>();
renderers[i].material.color = Color.red;
}
// Set green color to the first one
greenIndex = 0;
renderers[greenIndex].material.color = Color.green;
}
public void LightsEffectCore(float delay, bool changeDirection)
{
// Change color each `delay` seconds
if (Time.time > lastChangeTime + delay)
{
lastChangeTime = Time.time;
// Set color of the last renderer to red
// and the color of the current one to green
renderers[greenIndex].material.color = Color.red;
if (changeDirection == true)
{
Array.Reverse(renderers);
changeDirection = false;
}
greenIndex = (greenIndex + 1) % renderers.Length;
renderers[greenIndex].material.color = Color.green;
}
}
}
In this script i want to use the enum Directions instead the bool in the core method:
public void LightsEffectCore(float delay, bool changeDirection)
Instead bool changeDirection to use the enum to decide what direction Forward or Backward.
And then to use this script in another one for example in another script i did:
public LightsEffects lightseffect;
Then in Start:
void Start()
{
lightseffect.LightsEffect(objects, Color.red, Color.green);
}
Then in Update i want to decide the direction:
void Update()
{
lightseffect.LightsEffectCore(0.1f, false);
}
But instead false/true i want to use also here Forward/Backward
Maybe something like:
lightseffect.LightsEffectCore(0.1f, lightseffect.Forward);
Since false/true not really have a meaning of what it's doing.
Another option is to leave it with false/true but to add a description to the method when the user typing: lightseffect.LightsEffectCore(
When he type ( he will see a description of what the method do and when he will type ,
It will tell him what the false/true will do. But i'm not sure how to make a description for that.
Directions is the name of the enum you're interested in. It's also contained within the LightsEffects class.
Thus the way to reference it somewhere else would be LightsEffects.Directions and then another dot and then the value (Forward,etc). This is similar to a Fully Qualified Name, but with part of that scope already declared (whatever import/using that gets you LightsEffects).
So your line:
lightseffect.LightsEffectCore(0.1f, lightseffect.Forward);
Becomes:
lightseffect.LightsEffectCore(0.1f, LightsEffects.Directions.Forward);
Depending on your IDE, it should even be able to resolve this for you if you had used Directions.Forward: your IDE would have noticed the error (unknown reference Directions) and suggested a fix (change to LightsEffects.Directions), I know that Visual Studio does this, as I have a similar case in a project I'm working on now.

how to make an ammo limit decrease from its current value, after it has been increased in C#, Unity?

my character shoots arrows. She starts without zero arrows and cannot shoot any until she picks up an arrow icon. Arrow icons have a value of 3. After this she can shoot arrows. That code works fine. Now I have to make it so these arrows decrease in value through the UI text display. The UI text value changes from 0 to 3 when an arrow icon is picked up, but it doesn't decrease when I shoot an arrow. I have another game object with a script that will detect when an arrow is shot. when this happens, it tells that main script that, "hey, an arrow was just shot." The focus is on getting the Text to decrease when I shoot an arrow.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class arrowManager : MonoBehaviour {
private Text text;
public static int arrowCount;
public static int arrowRecount;
private int maxArrows = 99;
void Start ()
{
text = GetComponent<Text> ();
arrowCount = 0;
}
void Update ()
{
FullQuiver ();
arrowCounter ();
}
void arrowCounter()
{
if (arrowCount < 0) {
arrowCount = 0;
text.text = "" + arrowCount;
}
if (arrowCount > 0)
text.text = "" + arrowCount;
}
public static void AddPoints(int pointsToAdd)
{
arrowCount += pointsToAdd;
}
public static void SubtractPoints(int pointsToSubtract)
{
arrowCount -= pointsToSubtract;
}
public void FullQuiver()
{
if (arrowCount >= maxArrows)
{
arrowCount = maxArrows;
}
}
}
the game object with the script that detects arrows looks like this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class arrowDetector : MonoBehaviour {
public int pointsToSubtract;
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "arrow")
{
arrowManager.SubtractPoints (pointsToSubtract);
}
}
}
Forgive me if I've misunderstood, but it looks to me like you are subtracting from the wrong variable.
Since you are displaying the 'arrowCount' variable, I imagine that's what should be subtracted from.
public static void SubtractPoints(int pointsToSubtract)
{
if (arrowCount > 0) {
arrowCount -= pointsToSubtract;//pointsToSubtract is an int value passed to this script from my player script whenever she shoots an arrow.
}
}
In SubtractPoints method you are detracting from the variable "arrowRecount".
Wouldn't you want to be subtracting from "arrowCount" instead? If you used "arrowCount" your text value should update properly.
I figured it out. Before, I was trying to get it to detect my arrows whenever a bool became true from my player script. That wasn't working so I said screw it and just made an empty that detects gameobjects with the tag "arrow." Then I updated the script in here to reflect that. I was dead tired last night after not getting any sleep for two days so I forgot to put in a value of pointsToSubtract in the hierarchy. Thanks everyone for their responses.

Dragging Item With Icon Following Pointer Unity C# [duplicate]

This question already has answers here:
Unity3D UI, calculation for position dragging an item?
(4 answers)
Closed 6 years ago.
How To Dragging Item With Icon Following Pointer ?
MAYBE IT WILL TAKE SOME TIMES TO SEE ALL THE CODE. Many thanks Before for your attention.
But I have done a little Code Before. And it is run successful, but there is some mistake in my code. Actually the icon dragging is following the pointer but it just temporary.
And my prediction is because i make an paging inventory Storage 1 , 2 , and 3. When All of 3 Storgae is visible the dragging item icon run successfull with following the pointer. But if one of the inventory storage was hide for example : i have click storage 2 then storage 1 and 3 are hide then when i dragging the item the icon was not following the pointer. It become Stuck. But still can swap item slot. Just only the icon not following the pointer.
Below is my code it a little long :) I will give the detail code.
slotScript.cs (this script is a prefabs of Slot that show item icon, amount)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
public class slotScript : MonoBehaviour, IPointerDownHandler, IPointerEnterHandler, IPointerExitHandler, IDragHandler {
Image itemImage;
Sprite icon;
Text amount;
public int slotNumber;
inventory inventoryx;
player Playerx;
item temp;
menu menux;
dragitemicon dragger;
// Use this for initialization
void Start () {
inventoryx = GameObject.FindGameObjectWithTag ("inventory").GetComponent<inventory> ();
itemImage = gameObject.transform.GetChild (0).GetComponent<Image> ();
amount = gameObject.transform.GetChild (1).GetComponent<Text> ();
}
// Update is called once per frame
void Update () {
//Show Images Icon Slot Inventory
if (inventoryx.Player.items[slotNumber].itemName != null) {
itemImage.enabled = true;
icon = Resources.Load<Sprite> (inventoryx.Player.items[slotNumber].itemIcon + "/" + inventoryx.Player.items[slotNumber].itemName);
itemImage.sprite = icon;
amount.text = inventoryx.Player.items[slotNumber].itemStock.ToString();
amount.enabled = true;
} else {
itemImage.enabled = false;
}
}
public void OnPointerDown (PointerEventData data) {
if (inventoryx.Player.items[slotNumber].itemType == item.ItemType.Raw) {
inventoryx.Player.items [slotNumber].itemStock--;
}
//Debug.Log (transform.name);
if (inventoryx.Player.items [slotNumber].itemName == null && inventoryx.draggingitem) {
inventoryx.Player.items [slotNumber] = inventoryx.getdragitem;
inventoryx.closeDragItem ();
//inventoryx.dragitemicon.GetComponent<Image>().sprite = null;
} else if (inventoryx.draggingitem && inventoryx.Player.items[slotNumber].itemName != null ) {
inventoryx.Player.items[inventoryx.indexofdragitem] = inventoryx.Player.items[slotNumber];
inventoryx.Player.items[slotNumber] = inventoryx.getdragitem;
inventoryx.closeDragItem ();
}
}
//On Drag Close ToolTips and Show Drag Item
public void OnDrag (PointerEventData Data) {
if (inventoryx.Player.items[slotNumber].itemType == item.ItemType.Raw) {
inventoryx.Player.items[slotNumber].itemStock++;
}
if (inventoryx.Player.items[slotNumber].itemName != null) {
inventoryx.showDragItem(inventoryx.Player.items[slotNumber], slotNumber);
dragger.showDragItem(inventoryx.Player.items[slotNumber], slotNumber);
inventoryx.Player.items[slotNumber] = new item();
inventoryx.closeToolTips();
amount.enabled = false;
}
}
}
How to change dragging code above i used to this code below :
public class Draggable : MonoBehaviour,
IBeginDragHandler, IDragHandler, IEndDragHandler {
public void OnBeginDrag(PointerEventData eventData) {}
public void OnDrag(PointerEventData eventData) {
//Debug.Log ("OnDrag");
transform.position = eventData.position;
}
public void OnEndDrag(PointerEventData eventData) {}
}
Many Thanks
Dennis
Very fortunately, there is now an
incredibly simple way
to do this in Unity.
Here it is: https://stackoverflow.com/a/37473953/294884
Ask if you have more trouble!

Categories

Resources