简体   繁体   中英

“Object reference” needed in script

I am currently working on a foxes & rabbits simulation, and I am completely stuck on "breeding".

The way I have built the simulation, three scripts are used; “TheGame”, “FoxScript” and “RabbitScript. Since the foxes and rabbits are essentially the same, we can reduce these three to two scripts; “RabbitScript” and “TheGame”. The RabbitScript is attached to the respective prefab; the “rabbitPrefab”, whereas TheGame is attached to an empty GameObject.

TheGame instantiates a number of RabbitPrefabs, which then move, age and breed. Since the build is supposed to collect and present data at a later stage, the rabbits are included in a list as well as being counted. This list is found in the main script, and when the rabbits breed, the offspring needs to be included in this list as well as adding to the counter.

I have tried instantiating a primitive with this method, and it works.

The Breed function in the script attached to the rabbits:

void Breed(){   
    float p = Random.Range (0.0f, 1.0f); 
    if (p < probability2breed) {        
            position = gameObject.transform.position;
            TheGame.BreedRabbit(position);  
    }
}

And the BreedRabbit method in TheGame script:

public static void BreedRabbit(Vector3 position)  {
    GameObject rabbit = Instantiate(RabbitPrefab) as GameObject; 
    rabbit.transform.position = new Vector3(position); 
    Rigidbody gameObjectsRigidBody = rabbit.AddComponent<Rigidbody>(); 
    rabbit.GetComponent<Rigidbody>().useGravity = false;
    rabbit.name = "Rabbit#:" + rabbitCount; 
    rabbit.tag = "rabbittag"; 
    rabbits.Add(rabbit); 
    rabbitCount++;
} 

NOTES: (I figure a lot of this code seems pointless, so to answer any questions about that beforehand: I use collider to handle interactions between the agents involved, and to my understanding this calls for a rigidbody. With rigidbody they started falling, even without mass, so I had to turn of gravity. The tags are to my understanding needed for collision handlig as well.I could probably skip the count and just count the list, but this shouldn't matter now)

It keeps asking for an object reference , and I just can't figure out how this can be solved?

THe error message: "an object reference is required for the nonstatic field method or property"

I'd assume the object reference error occurs on this line?:

GameObject rabbit = Instantiate(RabbitPrefab) as GameObject;

If this is the case, it may be because the prefab hasn't been set, ie, the script doesn't know what RabbitPrefab is.

You could set a variable in the script, and then drag your prefab onto the corresponding slot into the inspector:

public GameObject theRabbitPrefab;

GameObject rabbit = Instantiate(theRabbitPrefab) as GameObject;

If this is not the case, can you edit your question to where you are getting the error? Surely the error states which line of code the error is being generated from? :)

Edit: From Diego, if this is the case, you can add the rigidbody and configure it in your prefab, and you won't need to do it in the code for every new rabbit!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM