- Home /
Small error C#
Hello
I have been trying to get the following code to work but i have been getting this error Assets/BreakingGlass.cs(17,4): error CS0103: The name go does not exist in the current context
I have no clue how to fix this. Thanks
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BreakingGlass : MonoBehaviour {
public GameObject PortalB;
public bool Active = false;
void Update() {
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Wrench") {
PortalB.SetActive(false);
GameObject[] gos = GameObject.FindGameObjectsWithTag ("Glass");
Active = true;
foreach (GameObject go in gos);
gos.Rigidbody.iskinematic = false;
PortalB.SetActive(false);
for(int i=0; i<gos.Length; i++)
{
for(int j=0; j<gos[i].transform.childCount; j++)
{
gos[i].transform.GetChild(j).gameObject.active = false;
}
}
}
}
}
Edit: I did a test and when i threw the wrench at the object nothing happens. Can i get help with this to please
Well
foreach (GameObject go in gos);
Doesn't do anything, probably your error. A foreach loop works like any loop, it would look like this.
foreach (GameObject go in gos)
{
go.doStuff;
}
Answer by TeohRIK · May 11, 2017 at 03:29 AM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BreakGlass2 : MonoBehaviour
{
public Rigidbody body;
public Rigidbody[] Shads;
public void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Wrench")
{
Debug.Log ("Hi");
foreach (Rigidbody body in Shads) {
body.isKinematic = false;
}
}
}
}
Thanks for the replay but im still getting this error Assets/BreakingGlass.cs(20,35): error CS1061: Type UnityEngine.Rigidbody does not contain a definition for iskinematic and no extension method iskinematic of type UnityEngine.Rigidbody could be found. Are you missing an assembly reference?
opps...forget to change the iskinematic to is$$anonymous$$inematic and I fixed some mistake
foreach (GameObject go in gos){ Rigidbody rb = go.GetComponent<Rigidbody>(); if(rb != null) rb.is$$anonymous$$inematic = false; else Debug.Log("Rigidbody is Null"); }
That is because he missed the capital $$anonymous$$ in is$$anonymous$$inematic. Change iskinematic to is$$anonymous$$inematic. @walaoRI$$anonymous$$I nvm sorry, I see that you only copied the OP's code and it was not your missing the capital, but his. Either way, problem solved :)
Thanks for all the help. I am no longer getting any errors but nothing seems to be happing when the bullet coll with the window. Any ideas? ps the right tag is set on the object
Well now that you mention the intent of this script, then it makes zero sense how you set it up... What is going on? Why do you GameObject.FindGameObjectsWithTag ("Glass");
? If your bullet hits the glass, either the bullet or the glass should handle the collision. Assu$$anonymous$$g it is the glass handling it, because the collision is checking for a bullet, why are you searching for glass? It should look more like this.
public class Glass : $$anonymous$$onoBehaviour
{
public Rigidbody body;
}
then your window
public class Window : $$anonymous$$onoBehaviour
{
public Glass[] glass;
public void OnCollisionEnter(Collider other)
{
if (other.gameObject.tag == "Bullet")
{
foreach (Glass _glass in glass)
{
glass.body.is$$anonymous$$inematic = false;
}
}
}
}
$$anonymous$$y window is a solid see through object that will be deleted and replaced by a shattered window (when the bullet hits it) with about 200 children so i gave them the tag window so i could turn off is$$anonymous$$inematic at the same time for all of them
You need to create the Glass class first, then you can just use the Add Componenet button.
Actually, you could just add a Rigidbody[] glass
field to your Window class, and then lock the window inspector, mass select the glass pieces, and drag and drop them into the array.
Then you can just iterated the rigidbodies like so
foreach (Rigidbody body in glass)
{
body.is$$anonymous$$inematic = false;
}
This is probably the most efficient thing to do.
Sorry i don't really know what i am meant to do
Will giv you some tips later, juz come b from work now going for some running. whats your current country time?
@flatalex123 use Collision not Collider, collider only for OnTriggerEnter, for OnCollisionEnter you need to use Collision
public void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Wrench")
{
Debug.Log ("Hi");
foreach (Rigidbody body in Shads) {
body.is$$anonymous$$inematic = false;
}
}
}
Answer by Kristinosis · May 11, 2017 at 01:58 PM
You need to get rid of the semi-colon after your foreach loop. "go" doesn't exist outside of the loop and the semi-colon is ending the loop statement. :)
Your answer
Follow this Question
Related Questions
Unity 5 UNet Spawn as Child. 1 Answer
The name 'TrackManager' does not exist in the current context [Unity Vuforia Ground Plane Project] 0 Answers
the object of type rigidbody has been destroyed 3 Answers
Error building Player because scripts had compiler errors 2 Answers
script does not update inspector? 3 Answers