- Home /
how to change gameobject variable to a different gameobject in c#
Hi guys! I thought that this would be relatively simple but I have spent hours trying to figure out how I can change a GameObject variable to another GameObject in C#.
Here is my current code :
using UnityEngine;
using System.Collections;
public class infect : MonoBehaviour {
public GameObject replicationClass;
void OnCollisionEnter(Collision collision) {
if (collision.transform.GetComponent<stats> ().status == 1) {
collision.transform.GetComponent<stats> ().status = 2;
collision.transform.GetComponent<replicate> ().replication = replicationClass;
Destroy (gameObject, (Time.deltaTime * 20));
}
}
}
I am trying to change the replication GameObject of the replication script, to the replicationClass GameObject variable defined on line 6. However all this does is set replication (in the replication script) to null and gives me the
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.
error when I run the program. I am very confused as to why this does not work.
Below is my replication script :
using UnityEngine;
using System.Collections;
public class replicate : MonoBehaviour {
public float timeLeft = 30.0f;
public float requiredTime = 30.0f;
public GameObject replication;
public GameObject WorldScript;
void Awake(){
timeLeft = requiredTime;
}
void Update()
{
timeLeft -= Time.deltaTime;
if(timeLeft < 0)
{
timeLeft = requiredTime;
Debug.Log (replication);
if(WorldScript.GetComponent<worldStats>().currentCells < WorldScript.GetComponent<worldStats>().maxCells)
Instantiate(replication, gameObject.transform.FindChild("spawnPoint").position, transform.rotation);
}
}
}
Does anybody know what I'm doing wrong here? Thank you very much for your time and help =) I would appreciate it if all code you showed me was in C#
Hello, need some information to answer your question, what is replicationClass
object? Is it a prefab or a scene object. You may get this exception if this object is a child of infect
game object that gets destroyed. From the first glance you have issues with hierarchy.
http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling
Try to adapt your script to use object pooling ins$$anonymous$$d. Assu$$anonymous$$g you have a lot of gameobjects, it will QUIC$$anonymous$$LY become too processor heavy to destroy/instantiate new objects.
Anyway: why are you destroying infect? Please provide info on this.
Sorry for the confusion guys. replicationClass is a gameObject (its itself, actually).
Answer by karma0413 · Jan 29, 2015 at 09:49 PM
I am a newbie. But I have had some problem between understanding TYPES.
An easy way for me to get the right one is 2 ways:
Understanding types... While you defined replicationClass correctly, isn't it technically a wrong name...... maybe you should call it replicationObject, because after all this variable you defined as a GameObject. Now, if you have this (infect) script attached to some gameobject on the scene... Then you simply define it different..... lets call it replicationObject instead please.. and use this code in it's place...
//Defining a parameter int a = replicationObject.GetComponent <Infect> ().somevariablethatisinsidetheinfectscript; //Calling a method bool didHeInfectSomeone = replicationObject.GetComponent <Infect> ().IInfectedSomeone (); //and then inside the infect, you can set a method after update that says something like: public bool IInfectedSomeone () { return myInfectionStatus; // we may have set this to false initially, or whatever.. hopefully you see what I am doing with the script }
So hopefully as you can see. When we want to access the Class, we simply access it as part of a component of the actual gameobject in the scene. Because that's all scripts really are.
I am not the best about STATIC scripts. but if you research how to make a Class script STATIC... Then you can access it a little easier. and intead use phrasing like:
int a = Infect.somevariableIhaveinsideThisScript;
if (Infect.IInfectedSomeone)
{
print ("Holy cow, he infected someone");
}
the 2nd way
to access understanding it... Is using the drag and drop method in the Unity Editor.
As you have your code currently setup... All you have to do is attach the Infect script to "some game object inside the scene". And then inside the UnityEditor for drag in the Object that you added the infect script to.... It's that simple.
But again, when calling it... You will still have to call it as a GetComponent of that object.
For collisions, they are obviously another component of the Gameobject...
collision.GameObject.GetComponent <Infect> ().variable = 2;
Answer by moonstruck · Jan 30, 2015 at 08:46 PM
If replicationClass
is the object itself than the problem is obvious: you assign the reference to this object to another script and destroy the object then try to Instantiate
from that destroyed object. Try changing replicationClass
to a prefab reference (probably created from the same object) and it will work.
P.S. You have a lot of GetComponent
's in your code, they make the code less readable and most likely less efficient, try using variables to store these references like:
void OnCollisionEnter(Collision collision) { stats s = collision.transform.GetComponent(); if (s.status == 1) { s.status = 2; collision.transform.GetComponent ().replication = replicationClass; Destroy (gameObject, (Time.deltaTime * 20)); } }
Also it would be cool changing WorldScript
variable type to worldStats
since you don't use operation on the object itself, only that script.
Your answer
Follow this Question
Related Questions
How to run a function in a GO that have DontDestroyOnLoad 1 Answer
C# GetComponent / change values throught other script 3 Answers
Null Reference Exception Help 1 Answer
Multiple Cars not working 1 Answer
GetComponent error 2 Answers