PointerClick only works once - c#

I have a problem & it's annoying me.
I have a prefab that containing just one UI Image (supposed to be square). I'm trying to create (via script) many of them (Like 5x5 or 10x10). I added a pointerClick event with eventTrigger for detect click & print the clicking objects' name. But there is a problem. PointerClick event only works once. It is supposed to give result for every click. Thanks for helping.
Here is my prefabs' photo & my scripts:
prefab_photo
Creation
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class gameSlots : MonoBehaviour {
public GameObject gameSlot;
public int gameLevel=5;
void Start(){
gameLevel = PlayerPrefs.GetInt ("levelSelect");
RectTransform gameTable=gameObject.GetComponent<RectTransform>();
float width = gameTable.rect.width/gameLevel;
for (int i=0; i<gameLevel; i++){
for(int j=0;j<gameLevel;j++){
GameObject slot=(GameObject)Instantiate(gameSlot);
slot.transform.SetParent(gameObject.transform,false);
slot.name=(i+1)+"-"+(j+1);
RectTransform rectTrans = slot.GetComponent<RectTransform>();
float x= -gameTable.rect.width/2 + width*i;
float y= gameTable.rect.height/2 - width*j-width;
rectTrans.offsetMin=new Vector2(x,y);
x= rectTrans.offsetMin.x + width;
y= rectTrans.offsetMin.y + width;
rectTrans.offsetMax=new Vector2(x,y);
}
}
}
script for PointerClick event
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class slotCheck : MonoBehaviour {
public void name(){
Debug.Log (transform.name);
}
}

Related

Unity C# dialogue system script displays only first letter of public var

I have a pretty strange problem. I was coding a simple dialogue system, for now it will only display a letter every 0.1 seconds.
I have a TextMeshPro text that contains the text to be displayed.
Here is attached my DialogueLine.cs script:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
namespace DialogueSystem
{
public class DialogueLine : DialogueBaseClass
{
private TextMeshProUGUI textHolder;
[Header ("Text Options")]
[SerializeField] private string input;
private void Awake()
{
textHolder = GetComponent<TextMeshProUGUI>();
StartCoroutine(WriteText(input, textHolder));
}
}
}
it gets the value of my "input" variable, setted from Unity interface. The script calls the "WriteText" coroutine, in the DialogueBaseClass.cs script:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class DialogueBaseClass : MonoBehaviour
{
protected IEnumerator WriteText(string input, TextMeshProUGUI textHolder)
{
for (int i= 0; i < input.Length; i++)
{
textHolder.text += input[i];
yield return new WaitForSeconds(0.1f);
}
}
}
A pretty simple script that adds on my "textHeader" component a letter from the input every 0.1 seconds.
But, here is the problem. The output of my component is only the first letter of the input variable: if in the input I write "Hello" it displays "H".
I really can't find the problem in this script...

Unity c# change light temperatur with script

im trying to write a script that makes the temperature of the light whatever number is set in the float
i need this for later reference when i add a slider or selectable options that determine the temperature of the light
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class lightchange : MonoBehaviour
{
public float Temp = 2000f;
private void pp()
{
Light lightcomp = GetComponent<Light>();
lightcomp.colorTemperature = Temp;
}
}
this is my code but it doesnt seem to work and this is all i could find online to edit componentswhat im trying to change

Why does setting my Rotate.z change all variables?

Im trying to make a sprite rotate onscreen to show the player how their Jet is rotated while they fly.
Here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AircraftScript : MonoBehaviour
{
public float Altitude;
public Text AltitudeText;
public GameObject AircraftImage;
public GameObject Aircraft;
void Update()
{
Altitude = Aircraft.GetComponent<Rigidbody>().position.y;
AltitudeText.text = Altitude + "ft";
AircraftImage.Rotate.z = Aircraft.Rotate.z;
}
}
It changed all the values of the Sprite (x,y,z) instead of just the z.
I did also try
AircraftImage.Rotate = new Vector3(0 , 0 , Aircraft.Rotate);
and
AircraftImage.Rotate = new Vector3(0 , 0 , Aircraft.Rotate.z);
neither worked, what have I done wrong?
I don't understand your code: Aircraft and AircraftImage are GameObjects, and GameObjects don't have the Rotate method, so that code should throw an exception.
Anyway I try to help you anyway.
Try using this code:
AircraftImage.GetComponent<RectTransform>().eulerAngles = new Vector3(0, 0, Aircraft.transform.eulerAngles.z);

GameObject follow canvas image position

I have a gameObject that I would like to move between X=-2 and X=+2. It follows the position of a canvas Image that also moves in X axis as X=12 and X=150. How do I make sure my gameObject follows the right axis of the image without parenting it? It should be clamped between -2 & +2. So how do I do this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowImagePosition : MonoBehaviour
{
public GameObject followImage;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void LateUpdate()
{
this.transform.position = new Vector3(followImage.transform.position.x, this.transform.position.z, this.transform.position.z);
}
}

using fade out effect to load scene in Unity, but it doesn't work at all

Now I'm making C# script to load scene after fade out effect.
First scene has a panel and the tag of the panel is image.
The name of the second scene is EndingCredit.
But when I added this script to panel(panel is below the canvas) and clicked play button,
it didn't work at all.
The code is below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
/*
* script explanation
* shows Earth image and fade out
* after fade out effect, load EndingCredit Scene
*/
public class LoadToCredit : MonoBehaviour
{
public Image image;
// Start is called before the first frame update
void Start()
{
//SceneManager.LoadScene("EndingCredit");
FadeEffect();
}
private IEnumerator FadeEffect()
{
float fadeCount = 0; //initial alpha value
while (fadeCount < 1.0f)
{
fadeCount += 0.01f; //lower alpha value 0.01 per 0.01 second
yield return new WaitForSeconds(0.01f); //per 0.01 second
image.color = new Color(0, 0, 0, fadeCount); //makes image look transparent
}
//after while loop ends, load EndingCredit scene
SceneManager.LoadScene("EndingCredit");
}
// Update is called once per frame
void Update()
{
//FadeEffect();
//SceneManager.LoadScene("EndingCredit");
}
}
I tested another code that using LoadScene().
And this script worked properly.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
/*
* script explanation
* shows Earth image and fade out
* after fade out effect, load EndingCredit Scene
*/
public class LoadToCredit : MonoBehaviour
{
public Image image;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
SceneManager.LoadScene("EndingCredit");
}
}
Here's some questions.
If the script is to fix, how should I fix it properly?
Is it okay to locate SceneManager.LoadScene("EndingCredit"); in FadeEffect?
What I want is that after the panel of the first Scene fades out, than after 2 seconds EndingCredit comes.
Thank you for reading this question.
Call FadeEffect as coroutine:
void Start() => StartCoroutine(FadeEffect());

Categories

Resources