I'm pretty sure this will be an easy fix, but I don't see it.
I want to change the texture on the material a projector is projecting.
This is what I have
using UnityEngine;
using System.Collections;
public class ShowPresentation : MonoBehaviour {
private GameObject SceneProjector;
private Material proj;
public Texture2D NewTexture;
void Start () {
SceneProjector = GameObject.FindGameObjectWithTag("Projector") ;
proj = SceneProjector.GetComponent<Projector>().material;
NewTexture = Resources.LoadAssetAtPath ("Assets/Textures/Wood.tga", typeof(Object)) as Texture2D;
proj.SetTexture("_MainTex", NewTexture);
}
void Update () {
Debug.Log (proj);
Debug.Log(NewTexture);
}
}
Everything used to be at update but even in Start it won't work.
The debug logs show that it can find the material and texture, so it has to be something to do with the settexture
Help would be much appreciated
i dont know what you did but this is the example from unity documentation. you can set it to the script of the object that you want to change its texture.
_BumpMap is the normal map
public Texture bumpMap;
renderer.material.SetTexture("_BumpMap", bumpMap);
Related
I'm new to Unity and C#. I'm trying to make the sprite of "bird" change when he dies in unity. I tried following some tutorials but it doesn't work, so now I'm just trying to make the sprite change when "A" is pressed, but it still doesn't work. Is it a problem of the script or of the sprite?
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeSprite : MonoBehaviour
{
public Sprite deadBird;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
GetComponent<SpriteRenderer>().sprite = deadBird;
}
}
}
Here is a screenshot: https://i.stack.imgur.com/PTd9W.png
I think #derHugo is correct, if you have an Animator then that will most likely overwrite any change you try and do. To fix this, you can use that animator and create an animation that is the deadBird. once you have that, connect it to the animator controller from the normal state. in that connection you can create and set a new animator boolean like "isDead" and set it to switch to the dead animation is the bool is true. then change your code from
GetComponent<SpriteRenderer>().sprite = deadBird;
To
anim = gameObject.GetComponent<Animator>() //place this instead of the bird sprite
anim.SetBool("isDead", true); //place this in the if statement
Hope that helps! it's much cleaner to go through the animator as it allows for easier changes as you build your game.
Im working on a game similar to archvale and enter the dungeon and I'm trying to create a dash function. Ive tried using force and some other methods I've searched for online but I haven't been able to find anything. Any ideas?
This game is a similar perspective to ETG being a 3D game using 2D sprites in a top down format.
Thanks!
EDIT: This is code I tried using but it didn't work at all. It is code I found online.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dash : MonoBehaviour
{
public class PlayerMovement : MonoBehaviour
{
public float dash = 3000f;
public bool DashTime = true;
// Use this for initialization
void Start()
{
bool DashTime = true;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("f"))
{
GetComponent<Rigidbody>().AddForce(new Vector2(dash, 0), ForceMode.Force);
StartCoroutine(LateCall());
}
}
IEnumerator LateCall()
{
yield return new WaitForSeconds(0.05f);
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionX;
yield return new WaitForSeconds(0.06f);
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
}
}
}
Without any code or anything to look at its kind of hard to answer the question. You could do a transform.translate or you could teleport based on where the player is looking or the mouse location. There are a ton of ways you could create a dash effect but without anything to go on it's really up in the air as to what solution you come up with.
You can check out this tutorial and it might help you:
https://generalistprogrammer.com/unity/unity-2d-dash-movement-effect-learn-to-how-to-tutorial/
I have a texture issue with a VR whiteboard. When I attach a texture to a plane in Unity that has the whiteboard.cs script attached, the whiteboard plane defaults to plain white when I press run. The plane is still reactive to the marker in a VR space, but I want to be able to put up a stencil texture/material that allows users to trace it. I've seen the similar issue when the variables are private and not when set to public. Has anyone experienced the same texture GetComponent issue? I'm using Unity 2021.2,10f.
Attached is a screenshot of the object before and after pressing run and the mentioned script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Whiteboard : MonoBehaviour
{
public Texture2D texture;
public Vector2 textureSize = new Vector2(x:2048, y:2048);
void Start()
{
var r = GetComponent<Renderer>();
texture = new Texture2D(width:(int)textureSize.x,
height:(int)textureSize.y);
r.material.mainTexture = texture;
}
}
I've written a script that is supposed to make tiling materials easier. I was tired of constantly re-adjusting the tiling settings of materials on my walls as I resized them.
[ExecuteInEditMode]
public class AutoTiler : MonoBehaviour
{
public float xScaleMult = 1;
public float yScaleMult = 1;
private Material texture;
// Update is called once per frame
void Update()
{
if (!texture)
{
texture = GetComponent<MeshRenderer>().material; //get the material if it is null
}
//set the material scale based on the size of the object
texture.mainTextureScale = new Vector2(Mathf.Max(transform.localScale.x, transform.localScale.z) * xScaleMult, transform.localScale.y * yScaleMult);
}
}
However, every time I reload the scene, I get an error message saying:
Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene. You most likely want to use renderer.sharedMaterial instead.
I've looked up this error but I can't quite figure out how to solve it. I understand that I'm copying the material in order to change it, but that is the desired result, as it needs different tiling settings for every wall, so that it does not appear stretched. Currently the code works, but fills the console with this error message. Is there a better way to do this?
in editor you can use sharedmaterial instead material property, something like this:
using UnityEngine;
[ExecuteInEditMode]
public class AutoTiler : MonoBehaviour
{
public float xScaleMult = 1;
public float yScaleMult = 1;
private Material material;
private Material Material
{
get
{
if (material == null)
{
MeshRenderer render = GetComponent<MeshRenderer>();
#if UNITY_EDITOR
material = render.sharedMaterial;
#else
material = render.material;
#endif
}
return material;
}
}
// Update is called once per frame
void Update()
{
//set the material scale based on the size of the object
Material.mainTextureScale = new Vector2(Mathf.Max(transform.localScale.x, transform.localScale.z) * xScaleMult, transform.localScale.y * yScaleMult);
}
}
for more details about difference between material and sharedmaterial see :
http://gyanendushekhar.com/2018/09/23/difference-material-shared-material-unity-tutorial/
https://docs.unity3d.com/ScriptReference/Renderer-sharedMaterial.html
Well, I'm trying to make an fps where you shoot tagets and appears an "Arcade-Style" score over them on unity 5, but, I don´t really know how to do it, already tried with an OnCollisionEnter(), but I did something wrong and it didn't worked, What can I do? Below you can see my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Diana : MonoBehaviour {
public GameObject diana;
public AnimationClip Score;
private Animation myAnimation;
/* IEnumerator Wait()
{
myAnimation = GetComponent<Animation>();
myAnimation.Play("DianaScore");
yield return new WaitForSeconds(3);
} */
void OnTriggerEnter(Collider other)
{
Debug.Log("Funcó wacho");
myAnimation = GetComponent<Animation>();
myAnimation.Play("DianaScore");
// StartCoroutine(Wait());
}
void Update () {
}
}
(Sorry for my bad english, I´m argentinian and new in all this coding thing)
I suggest putting this line of code: myAnimation = GetComponent(); into a Awake() function. If the above method does not work, I suggest checking that the Is Trigger is checked for OnTriggerEnter. If the console message (Debug.Log) shows, the problem might be something to do to the animation not the script. Last but not least, check that the bullet actually collides. I know this might sound crazy, but sometimes the bullet might be able to "not get detected" if it has enough velocity. Hope these suggestions work :D .