- Home /
I have a problem whit a script and prefabs
went i create a prefab the scrip assign in that prefab disappear event went i click applied a hundred time
before its a prefab
went it become a prefab
and after deleting and recreating sometime a message appear in the output saying Cyclic nesting detected
Answer by silvematt · Mar 08, 2019 at 10:35 PM
Prefabs are assets meant to use in the same way in every scene possible. Because of that, you can't assign a reference of a GameObject in a particular scene.
The only thing you can do to keep those references in the prefab is to make the gameObject that has 'Score' and 'BallMovement' as child of Bumper, but that's not too smart in this case.
Another workaround (that can be the best) is to use the 'Score' and 'BallMovement' as a Singleton and at the start of the BumperScript you get those references.
Last one, go with tags. Tag the Score gameObject with the tag 'Score' and with a simple
void Start()
{
Score = GameObject.FindWithTag("Score");
}
But i really suggest you to use the Singleton pattern.
Have a great dev-time!
Answer by antotecnho · Mar 09, 2019 at 03:43 AM
Thanks but its doesn't work because "Score" and "BallMovement" are script I reference the scripts because i want to read or change variable in those other script
here the bumper script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BumperScript : MonoBehaviour
{
public Animator animator;
public Score score;
public BallMovement BallMovement;
public int Bumper_State = 0;
public float CooldownTime = 60;
private float TimeLeft;
void Start()
{
TimeLeft = CooldownTime;
}
// Update is called once per frame
void Update()
{
animator.SetInteger("Bumper_State", Bumper_State);
if (Bumper_State == 0 & TimeLeft > 0)
{
score.AddPoints(1);
TimeLeft = CooldownTime;
}
else
{
TimeLeft--;
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (BallMovement.GameStart == true)
{
Bumper_State++;
}
}
}
Your answer
Follow this Question
Related Questions
Trying to have my Editor Script save the changed settings when I hit Play 0 Answers
How come I cannot set my prefab game component into a script public variable? 1 Answer
Unity project referencing another Unity project? 0 Answers
How can i add all the prefabs in the assets directory and sub directories to List or Array ? 0 Answers
Bullet Prefab spawning in multiple random positions and rarely the correct one (Unity 3D) 1 Answer