- Home /
How do keep the value of the Transform variable in script which is instantiated with a Game Object?
I am making a Player Detection System in c#. When the scene starts the game will instantiate the entity at a random point on the map. The problem is that for the Player Detection System to work, it needs to know the player's Transform coordinates to detect how far away the player is. The Transform is stored in a variable that looks like this: public Transform player; The issue is every time I instantiate the entity (The entity with the script on it), it resets the value that I set the variable to, in this case, the Player.
The rest of the script looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CotruxMovement : MonoBehaviour {
// Animation Control Variable
public Animator walk;
// Wandering Control Variables
public float moveSpeed = 3f;
public float rotSpeed = 100f;
public float chargeSpeed = 12f;
private bool isWandering = false;
private bool isRotatingLeft = false;
private bool isRotatingRight = false;
private bool isWalking = false;
// Attacking Control Variables
public Transform player;
public float attackBack;
public bool hostile = false;
private bool attacking = false;
// Knockback Variable
Rigidbody rigid;
void Start () {
rigid = GetComponent<Rigidbody>();
walk = GetComponent<Animator>();
}
void Update () {
// Wandering Controls Part 1
// ---
if (isWandering == false & !attacking)
{
StartCoroutine(Wander());
}
if(isRotatingRight == true & !attacking)
{
// Rotoate Right
transform.Rotate(transform.up * Time.deltaTime * rotSpeed);
walk.Play("CotruxIdle");
}
if (isRotatingLeft == true & !attacking)
{
// Rotate Left
transform.Rotate(-transform.up * Time.deltaTime * -rotSpeed);
walk.Play("CotruxIdle");
}
if(isWalking == true & !attacking)
{
// Moving Fowards
transform.position += transform.forward * moveSpeed * Time.deltaTime;
walk.Play("CotruxWalk");
}
// ---
// Player Detection & Attack Rotation
// ---
if (hostile == true)
{
if(Vector3.Distance(player.position, this.transform.position) < 33)
{
attacking = true;
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 0.5f);
transform.position += transform.forward * chargeSpeed * Time.deltaTime;
walk.Play("CotruxCharge");
} else {
attacking = false;
}
}
}
// Wander Timing
IEnumerator Wander()
{
int rotTime = Random.Range(1, 3);
int rotateWait = Random.Range(1, 3);
int rotateLorR = Random.Range(0, 3);
int walkWait = Random.Range(1, 3);
int walkTime = Random.Range(1, 5);
isWandering = true;
yield return new WaitForSeconds(walkWait);
isWalking = true;
yield return new WaitForSeconds(walkTime);
walk.Play("CotruxIdle");
isWalking = false;
yield return new WaitForSeconds(rotateWait);
if(rotateLorR == 1)
{
isRotatingRight = true;
yield return new WaitForSeconds(rotTime);
isRotatingRight = false;
}
if (rotateLorR == 2)
{
isRotatingLeft = true;
yield return new WaitForSeconds(rotTime);
isRotatingLeft = false;
}
isWandering = false;
}
// Player Collision Detection
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Player")
{
rigid.AddRelativeForce(-Vector3.forward * attackBack);
}
}
}
I also tried to set the variable to the Player by looking for the player's tag:
balls = GameObject.FindGameObjectsWithTag ("Ball").transform;
But this did not work.
Answer by Ady_M · Jan 05, 2019 at 03:51 PM
Next time, I would really advise you to post a piece of code that contains the bare minimum to illustrate the problem and show what you've tried. You'll greatly increase your chances of getting help.
Also, you haven't shown us the code that does the instantiation (unless I just missed it). If the instantiation occurs inside the script that has the "Transform player" property then simply do this:
var playerObject = instantiate (...);
player = playerObject.transform;
You seem to be using the wrong Find method. The one you used returns an array and then you tried to access "transform" inside the array which doesn't make sense.
Do it like this:
var ballGameObject = GameObject.FindWithTag ("Ball");
if (ballGameObject != null)
{
player = ballGameObject.transform;
}
else
{
Debug.Log ("Failed to find Ball object");
}
Thank you so much. The script now works perfectly. Sorry about not entering the bare $$anonymous$$imum. The script which I have submitted above is not the script that had the instantiate syntax in it but it was the script that was getting the most errors so I guessed it was this one.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Detect tag in C# 1 Answer
Finding the position of an object 1 Answer
how do i make an Enemy take damage from prefab bullet clone? 1 Answer