Change gameobject sprite from other scene - unity3d

I am new and learning coding. How do I change a sprite from another game scene through a buttoned code.
using UnityEngine;
public class PlayerChoice : MonoBehaviour
{
public GameObject plo;
public Sprite boy;
public Sprite girl;
public void Boy()
{
plo.GetComponent<SpriteRenderer>().sprite = boy;
}
public void Girl()
{
plo.GetComponent<SpriteRenderer>().sprite = girl;
}
}

You actually can't do it the way you are trying.
This is a bit tricky for a beginner and you would have to read a bit to get behind it.
This is some starting point where you can dive in:
You would have to to make a gameobject that lives between scenes with
DontDestroyOnLoad(targetGameObject);
see: https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
On this gameobject you could have a script which holds all variables that are needed in the next scene.
When scenes are switched access that object and get your values;

Related

How would I get the transform of a object in a list?

So I want to have a list of spawn points for my enemies to spawn from. The problem is that I cant get the transform of a spawn point and I don't understand any solutions online. They will all probably have different names too (spawnInFrontOfDoor, spawnInside1) so I won't be able to use GetObjectWithTag. Are there any solutions?
You can add a script into each of your spawn points:
public void SpawnPoint : MonoBehaviour{
public static List<SpawnPoints> spawnPoints = new List<SpawnPoints>();
void Start() => spawnPoints.Add(this);
public Transform GetTransform() => transform;
}
And wherever you need your list of spawn points, you can access through SpawnPoint.spawnPoints.
Example:
List<SpawnPoints> spawnPoints = SpawnPoint.spawnPoints;
Transform randomTransform = spawnPoints[Random.Range(0, spawnPoints.Count)].GetTransform();
Instantiate(enemyPrefab, randomTransform.position, Quaternion.identity);
If you want to access this list in Start method, change
void Start() => spawnPoints.Add(this);
to
void Awake() => spawnPoints.Add(this);
and the list should be ready to use in Start.
Save a list of your spawn points before hand, and then access that list later when you want to spawn in your enemies.
This is probably the most basic example.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
//If your game is not Procedurally Generated just drag and drop your spawn points onto this list.
public List<Transform> spawnPoints;
//Reference to your enemy.
public GameObject enemy;
void Start()
{
foreach(Transform spawn in spawnPoints)
{
SpawnEnemy(spawn);
}
}
//Method to spawn your enemy at a given point.
public void SpawnEnemy(Transform spawnPoint)
{
Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);
}
}
I have created a whole Enemy spawning system so if you need further detail or clarification don't be afraid to ask.

How to change a material in Unity using a script on collison

I am currently making a mobile game in Unity3D. I want it so that when a ball with the tag 'Damage' is collided with, it will change the material of a damage indicator at the top of the screen. Is there an easy way to do this?
Thank you in advance.
You would typically put something like "damage indicator" in your "GameManager" and a GameManager is usually a singleton. It means you on time of collision, you can check the collider tag, if it's "Damage" then you call a function in your GameManager to change the material of your "damage indicator". Something like this:
public class ExampleClass : MonoBehaviour
{
....
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Damage")
GameManager.instance.DamageDone();
}
}
GameManager:
public class GameManager : MonoBehaviour
{
public static GameManager instance;
public Material damageMat;
public Renderer damageIndicatorRenderer;
....
void DamageDone()
{
damageIndicatorRenderer.material = damageMat;
}
}

2D Ragdoll movement in unity

I tried make a Ragdoll game with my brother but
the problem is that we try make a Ragdoll movement and we can make a movement but
when we try this movement on the Ragdoll It just goes pretty normal. we know we need bones and things like that but we don't know how can we make A script like that. we don't want see tutorials on YouTube we want to make a script that how We want to be.
Anyone have any idea how we can do that?
Oh and I know when I wrote this there were some spelling errors in the above code so sorry for that.
here's our script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
// Start is called before the first frame update
public float force;
public Rigidbody2D body;
public Leg[] legs;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.D))
{
Vector2 vec = Vector2.right * force;
body.AddForce(vec);
foreach (Leg leg in legs)
{
leg.Walk();
}
}
}
}
[System.Serializable]
public class Leg
{
public Rigidbody2D bone;
public float walkRotation;
public float force;
public void Walk()
{
bone.MoveRotation(Mathf.LerpAngle(bone.rotation, walkRotation, force * Time.deltaTime));
}
}
Use animations for different movements it will save you a lot of time. If you want the Rag doll effect enable it after the Player dies. So, in animations you should check blend trees if you want different animations for different speeds or locations etc. All game characters move as one Object and the rest is done in Animations. It is much simpler and efficient.

NavMeshAgent is still moving after stopped in Unity3D

I was making a simple 3D project with Unity3D. But when using NavMeshAgent, I got a problem that NavMeshAgent was still moving even though it had been stopped. Why this situation is happening, and how can I get rid of this?
Below is GIF:
And this is a picture of NavMeshAgent of that red cube, if it needs:
This is a source code attached to that:
using UnityEngine;
using UnityEngine.AI;
public class PathFinder : MonoBehaviour {
public GameObject target;
private NavMeshAgent nav;
void Start() {
nav = GetComponent<NavMeshAgent>();
if (target != null) nav.SetDestination(target.transform.position);
nav.isStopped = true;
}
}

Unity: Distance between two game-objects in the scene view

I am new to Unity and as I am creating my interface, I am trying to find what's the distance between two game objects. I know how to do in a C#/javascript script, however I am strugling to find this information in the scene view. Any key I could press while moving an object to see the distance to its neighbours ?
In a script, you can use Vector3.Distance between vector3 to get the distance between two points. Every gameObject has a position that is represented in a Vector3.
Below is a script example that shows you how it works. You just have to drag the script onto a gameObject in your scene and drag in the inspector another gameobject in your scene. The script should run even when your not in play mode because of the [ExecuteInEditMode].This way you will see the distanceBetweenObjects update in real time without actually having to hit play.
using UnityEngine;
[ExecuteInEditMode]
public class DistanceBetweenTwoObjects : MonoBehaviour
{
public GameObject obj;
public float distanceBetweenObjects;
private void Update()
{
distanceBetweenObjects = Vector3.Distance(transform.position, obj.transform.position);
Debug.DrawLine(transform.position, obj.transform.position, Color.green);
}
private void OnDrawGizmos()
{
GUI.color = Color.black;
Handles.Label(transform.position - (transform.position -
obj.transform.position)/2, distanceBetweenObjects.ToString());
}
}
The method OnDrawGizmos will draw text in between the 2 objects showing the distance value to help make it more user friendly.
There isn't any built-in functionality for this, but it's fairly trivial to add such a readout into the display using [ExecuteinEditMode], which causes scripts to run even when the game is not in play mode. This script, for example, should readout the distances on different axes and in total:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class EditorDistanceDisplay : MonoBehaviour
{
public GameObject target1;
public GameObject target2;
public float distanceX;
public float distanceY;
public float distanceZ;
public float distanceTotal;
void Start()
{
}
void Update()
{
Vector3 delta = target2.transform.position - target1.transform.position;
distanceX = delta.x;
distanceY = delta.y;
distanceZ = delta.z;
distanceTotal = delta.magnitude;
}
}