Background loop doesn`t work properly in Unity - unity3d

Problem consist in incorrect background looping. Instead of scrolling the picture horizontally, it distort over time. This script correctly work with another material, but not with that one.
How it`s looks like (Right side is distorted):
I have this backgroundObject:
This background script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BackgroundLoop : MonoBehaviour
{
public float bgSpeed;
public Renderer bgRend;
void Update()
{
bgRend.material.mainTextureOffset += new Vector2(bgSpeed * Time.deltaTime, 0f);
}
}
Thanks for any help.

Select the texture in the project window.
Then in the inspector window set Wrap Mode to Repeat (or Mirror)
Click Apply

Related

How can i animate a String property in unity Animator?

I am creating an animation in my Unity Game using Playable Directors and Animators, and i need to animate the contents of a TextMeshPro so it changes its text within keyframe, but there is no Text property in the animator.
I initialy thought that this was just with TextMeshPro, and changed my component to a normal Text component. No property found either.
My last try was to create a new script called TextAnimator, and a string property that changes the TextMeshPro contents in editor using the [ExecuteInEditMode] annotation.
But when i tried to animate THIS property, it doesnt show up either!
Here is my component in game:
Here is when i try to add the string property:
Here is the TextAnimator:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
[ExecuteInEditMode]
public class TextAnimator : MonoBehaviour
{
public TextMeshProUGUI textUI;
[TextArea]
public string text;
void Update()
{
if (textUI) {
textUI.text = text;
}
}
}
It seems that Unity can't animate strings at all, why is that? And what can i do as a workaround to make my animations?
There is a simple solution without code. Consider using DOTween asset as alternative. Just add "DOTWeen Animation" component to your text object, set the animation type to "Text" and write the sentence that should gradually appear in "To" input field. See screenshot

Unity: How do I change the material of a prefab when it is not in the hierarchy?

I am building an AR app using AR Foundation but got stuck with changing the material of my model during runtime by pressing a button.
The script below works perfectly fine when I attach it to my model and then create buttons with OnClick() Events to access the specific material WHEN my model is in the hierarchy.
But I don't want it to stay in the hierarchy because otherwise it will constantly show up in AR. If I remove it from the hierarchy, the OnClick() is obviously missing a prefab.
So I need to have the possibility to still change materials from button clicks but without my model staying in the hierarchy. I think "AddListener" could be a solution, but I don't really know how to edit the script in order to make it work. With AddListener I wouldn't have to use OnClick().
Can anyone please show me the solution?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeColor : MonoBehaviour
{
public Material[] material;
Material setMaterial;
Renderer rend;
void Start()
{
rend = GetComponent<Renderer>();
rend.enabled=true;
}
void Update()
{
}
public void GreyMaterial()
{
rend.sharedMaterial = material[0];
setMaterial = rend.material;
}
public void YellowMaterial()
{
rend.sharedMaterial = material[1];
setMaterial = rend.material;
}
public void RedMaterial()
{
rend.sharedMaterial = material[2];
setMaterial = rend.material;
}
}
You can out your prefab in folder called "Resources". And get your prefab using Resources.Load in script next way:
GameObject prefab = Resources.Load<GameObject>("Path"); // Path WITHOUT "Resources/"
Here prefab is... your prefab and you get change some components and values in it.
P.S.
It is very important to write Resources letter to letter without mistakes
You may put folders into (folders into folders...) into Resources, but here you must set the exact path to your file.
I believe the script is not referencing all the new instantiated objects, its needs to reference them. Either store those new objects in a list that the script can change, or try to search for every item of that material you want to change when you run the change material function(not very efficient, but might be easier)
i found this on youtube

My Input Field Disappears and does not work unless I load a new scene and load the old scene back in Unity

I am making a customizable game using Unity 2019.3f. In my main menu, I have the option for people to input a number into an input field for the amount of ammo. The input field then converts that value to an int and sets a gameobject's transform position.x equal to it. For some reason, when I input a number into the input field and click apply settings, the input field completely disappears and I have to click Play and then go back to the main menu for the input field to reappear and then somehow start working and correctly assigning the right numbers to the right spots. I used Debug.Log to see if the value of the input field were convertible to an int but they were. My code to set the gameobject transform to the input field number is here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
public class GetInputValue : MonoBehaviour
{
public InputField player1ammoinput;
public InputField player2ammoinput;
public GameObject ammo1;
ammo1.transform.position = new Vector3(System.Convert.ToInt32(player1ammoinput.text), 0f, 0f);
ammo1.transform.localScale = new Vector3(System.Convert.ToInt32(player1ammoinput.text), 0f, 0f);
Debug.Log(player1ammoinput.text);
}
Can someone find out why unity is doing this? ammo1 is just the gameobject that stores the number. It is an empty Gamebject. The actual input field is a different gameobject which I refer to in this script as player1ammoinput.
If I understand this correctly you are parsing the text to an integer value, and then using that value to move the ammo1 object to the side (or towards the player).
I suggest moving the parsing to an extra variable before the actual adaptation, and think the value you get (say 22 ammo) will move the ammo1 item 22 units away, which is FAR, hence it disappears immediately from view.

How to render Text on custom mesh in unity3d which able to change text content at runtime?

I have struggle the problem two days ago. Most of solution is use CanvasRenderer, I also use it amostly achieve with the minial code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TextMeshCtrl : MonoBehaviour
{
public CanvasRenderer CanvasRenderer;
public Mesh meshModle;//standard Quad mesh just for test
// Update is called once per frame
void Update()
{
this.CanvasRenderer.SetMesh(meshModle);
}
}
But encounter problem is text not refresh when I change Text content.
I also test CanvasRenderer.Clear() at up of SetMesh, it clear text content, but not show text anymore.
By the way, I have another question, how to custom mesh to fit text rendering?Under Shaded Wireframe editor modle,I have seem every text char rendered as a seperation quad and I'm wonder how does unity engine render text with custom mesh and default(not has custom mesh)? Thanks

Unity: Is there a way to get all GameObjects/Components that were used in a Prefab?

A bit new to Unity, I know that you can get a prefab from the instance of the GameObject (right click on GameObject and Select Prefab). But is there a way to select the GameObjects and Components that were used to created a Prefab? (from the Prefab)
Try this example out. It contains two flavors of selection, one to select all components attached to the prefab, and the other to select just the monobehaviours attached to the prefab.
What this script will do is check what components / monobehaviours are attached to the currently selected GameObject (or prefab) and will output them to the log, as well as select them all.
Copy the below code, put it into a new script named "PrefabComponentSelectorEditor" and save it in a folder called "Editor".
You should then see a Menu Item called "AxS -> Prefab Component Selector". Click on it, and see the console as well as the inspector.
The code is commented, but in case you have any questions, feel free to ask.
P.S. Remember to use ONLY ONE of the flavours (i.e. Monobehvaiour or Component). Comment / Uncomment the code accordingly. I have left the component version uncommented, and the monobehaviour version is right below it.
using UnityEngine;
using UnityEditor;
using System.Collections;
public class PrefabComponentSelectorEditor : EditorWindow {
[MenuItem("AxS/Prefab Component Selector")]
public static void SelectComponents () {
Debug.Log ("GOs "+Selection.activeGameObject);
if(Selection.activeGameObject != null) {
//This will select all the COMPONENTS attached to the prefab
//Let's first get all the components this GameObject has
Component[] components = Selection.activeGameObject.GetComponents<Component>() as Component[];
//If the length of components is > 0 (always WILL be, since every GameObject is
//guarenteed to have a Transform Component at the very minimum
if(components.Length > 0) {
//Print all the components to console
foreach(Component component in components)
Debug.Log (component);
//Set the current selection to the components
Selection.objects = (Object[])components;
}
//This will select all the MONOBEHAVIOURS attached to the prefab
//First get all Monobehavious attached
//MonoBehaviour[] behaviours = Selection.activeGameObject.GetComponents<MonoBehaviour>() as MonoBehaviour[];
//Check if the length of the array is > 0. This is required, since a GameObject
//may have no scripts attached to it
//if(behaviours.Length > 0) {
//Print all attached Monobehvaiours to console
// foreach(MonoBehaviour behaviour in behaviours)
// Debug.Log (behaviour);
//Set the current selection to the Monobehaviours
// Selection.objects = (Object[])behaviours;
//}
//The difference between COMPONENT and MONOBEHAVIOUR
//A Monobehaviour is basically any script you make, so if you use the monobehaviour version
//it will only select the scripts you attach to the prefab
//A component is anything you can see on the inspector, including Unity's inbuilt components
//Essentially, all Monobehaviours are Components
//Eg. If your prefab has a single script called "ExampleScript" attached to it, and has a BoxCollider
//Monobehaviour version will output just the ExampleScript
//Component version will output the Transform, BoxCollider and ExampleScript
}
}
}